86bf3ce859
Add 'core::array::from_fn' and 'core::array::try_from_fn' These auxiliary methods fill uninitialized arrays in a safe way and are particularly useful for elements that don't implement `Default`. ```rust // Foo doesn't implement Default struct Foo(usize); let _array = core::array::from_fn::<_, _, 2>(|idx| Foo(idx)); ``` Different from `FromIterator`, it is guaranteed that the array will be fully filled and no error regarding uninitialized state will be throw. In certain scenarios, however, the creation of an **element** can fail and that is why the `try_from_fn` function is also provided. ```rust #[derive(Debug, PartialEq)] enum SomeError { Foo, } let array = core::array::try_from_fn(|i| Ok::<_, SomeError>(i)); assert_eq!(array, Ok([0, 1, 2, 3, 4])); let another_array = core::array::try_from_fn(|_| Err(SomeError::Foo)); assert_eq!(another_array, Err(SomeError::Foo)); ``` |
||
---|---|---|
.. | ||
benches | ||
primitive_docs | ||
src | ||
tests | ||
Cargo.toml |