Skip to content

Commit 480f850

Browse files
improve array_from_fn documenation
1 parent 3f2b2ee commit 480f850

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

library/core/src/array/mod.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ mod iter;
2323
#[stable(feature = "array_value_iter", since = "1.51.0")]
2424
pub use iter::IntoIter;
2525

26-
/// Creates an array `[T; N]` where each array element `T` is returned by the `cb` call.
26+
/// Creates an array of type [T; N], where each element `T` is the returned value from `cb`
27+
/// using that element's index.
2728
///
2829
/// # Arguments
2930
///
@@ -36,8 +37,18 @@ pub use iter::IntoIter;
3637
/// // elements to produce is the length of array down there: only arrays of
3738
/// // equal lengths can be compared, so the const generic parameter `N` is
3839
/// // inferred to be 5, thus creating array of 5 elements.
39-
/// let array = core::array::from_fn(|i| i);
40+
///
41+
/// let array: [_; 5] = core::array::from_fn(|i| i);
42+
/// // indexes are: 0 1 2 3 4
4043
/// assert_eq!(array, [0, 1, 2, 3, 4]);
44+
///
45+
/// let array2: [_; 8] = core::array::from_fn(|i| i * 2);
46+
/// // indexes are: 0 1 2 3 4 5 6 7
47+
/// assert_eq!(array2, [0, 2, 4, 6, 8, 10, 12, 14]);
48+
///
49+
/// let bool_arr: [bool; 5] = core::array::from_fn(|i| i % 2 == 0);
50+
/// // indexes are: 0 1 2 3 4
51+
/// assert_eq!(bool_arr, [true, false, true, false, true]);
4152
/// ```
4253
#[inline]
4354
#[stable(feature = "array_from_fn", since = "1.63.0")]

0 commit comments

Comments
 (0)