@@ -23,7 +23,8 @@ mod iter;
23
23
#[ stable( feature = "array_value_iter" , since = "1.51.0" ) ]
24
24
pub use iter:: IntoIter ;
25
25
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.
27
28
///
28
29
/// # Arguments
29
30
///
@@ -36,8 +37,18 @@ pub use iter::IntoIter;
36
37
/// // elements to produce is the length of array down there: only arrays of
37
38
/// // equal lengths can be compared, so the const generic parameter `N` is
38
39
/// // 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
40
43
/// 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]);
41
52
/// ```
42
53
#[ inline]
43
54
#[ stable( feature = "array_from_fn" , since = "1.63.0" ) ]
0 commit comments