rust/library/core
Guillaume Gomez 86bf3ce859
Rollup merge of #75644 - c410-f3r:array, r=yaahc
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));
 ```
2021-10-09 17:08:38 +02:00
..
benches benchmark for str.chars().count() 2021-09-11 00:25:41 +02:00
primitive_docs Add primitive documentation to libcore 2021-09-12 02:23:08 +00:00
src Rollup merge of #75644 - c410-f3r:array, r=yaahc 2021-10-09 17:08:38 +02:00
tests Rollup merge of #75644 - c410-f3r:array, r=yaahc 2021-10-09 17:08:38 +02:00
Cargo.toml rfc3052: Remove authors field from Cargo manifests 2021-07-29 14:56:05 -07:00