pub fn try_from_fn<R, const N: usize, F>(
cb: F,
) -> <<R as Try>::Residual as Residual<[<R as Try>::Output; N]>>::TryType
ð¬This is a nightly-only experimental API. (array_try_from_fn
#89379) Expand description
Creates an array [T; N]
where each fallible array element T
is returned by the cb
call. Unlike from_fn
, where the element creation canât fail, this version will return an error if any element creation was unsuccessful.
The return type of this function depends on the return type of the closure. If you return Result<T, E>
from the closure, youâll get a Result<[T; N], E>
. If you return Option<T>
from the closure, youâll get an Option<[T; N]>
.
cb
: Callback where the passed argument is the current array index.#![feature(array_try_from_fn)]
let array: Result<[u8; 5], _> = std::array::try_from_fn(|i| i.try_into());
assert_eq!(array, Ok([0, 1, 2, 3, 4]));
let array: Result<[i8; 200], _> = std::array::try_from_fn(|i| i.try_into());
assert!(array.is_err());
let array: Option<[_; 4]> = std::array::try_from_fn(|i| i.checked_add(100));
assert_eq!(array, Some([100, 101, 102, 103]));
let array: Option<[_; 4]> = std::array::try_from_fn(|i| i.checked_sub(100));
assert_eq!(array, None);
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4