Skip to content

Commit 1fc83ae

Browse files
authored
Rollup merge of #104839 - HintringerFabian:docs_array_from_fn, r=scottmcm
improve array_from_fn documenation Improves array::from_fn documentation Fixes #102609 There were also unresolved comments from [this PR #100462](#100462), which have been added to my PR
2 parents 42010a2 + 69d562d commit 1fc83ae

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

library/core/src/array/mod.rs

+12-1
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.
40+
///
3941
/// let array = 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: [usize; 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 = core::array::from_fn::<_, 5, _>(|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)