A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://doc.rust-lang.org/nightly/std/array/fn.try_from_fn.html below:

try_from_fn in std::array - Rust

Function try_from_fnSource
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]>.

§Arguments §Example
#![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