Skip to content

Commit 5182758

Browse files
committed
Initial implementation
1 parent 3044419 commit 5182758

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

library/core/src/array/mod.rs

+16
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,22 @@ array_impl_default! {32, T T T T T T T T T T T T T T T T T T T T T T T T T T T T
389389

390390
#[lang = "array"]
391391
impl<T, const N: usize> [T; N] {
392+
/// Returns the number of elements in the array.
393+
///
394+
/// # Examples
395+
///
396+
/// ```
397+
/// #![feature(array_len)]
398+
/// let a: [usize; 3] = [1, 2, 3];
399+
/// assert_eq!(a.len(), 3);
400+
/// ```
401+
#[doc(alias = "length")]
402+
#[unstable(feature = "array_len", issue = "none")]
403+
#[inline]
404+
pub const fn len(&self) -> usize {
405+
N
406+
}
407+
392408
/// Returns an array of the same size as `self`, with function `f` applied to each element
393409
/// in order.
394410
///

library/core/tests/array.rs

+6
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,12 @@ fn empty_array_is_always_default() {
288288
let _arr = <[DoesNotImplDefault; 0]>::default();
289289
}
290290

291+
#[test]
292+
fn array_len() {
293+
let a = [1, 2, 3];
294+
assert_eq!(a.len(), 3);
295+
}
296+
291297
#[test]
292298
fn array_map() {
293299
let a = [1, 2, 3];

library/core/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![feature(alloc_layout_extra)]
22
#![feature(array_chunks)]
33
#![feature(array_methods)]
4+
#![feature(array_len)]
45
#![feature(array_map)]
56
#![feature(array_windows)]
67
#![feature(bool_to_option)]

0 commit comments

Comments
 (0)