diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index 37af3557fdd51..7dec9328eebd3 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -389,6 +389,23 @@ 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 #[lang = "array"] impl [T; N] { + /// Returns the number of elements in the array. + /// + /// # Examples + /// + /// ``` + /// let a: [usize; 3] = [1, 2, 3]; + /// assert_eq!(a.len(), 3); + /// ``` + #[cfg(not(bootstrap))] + #[doc(alias = "length")] + #[stable(feature = "rust1", since = "1.0.0")] + #[rustc_const_stable(feature = "array_len", since = "1.53.0")] + #[inline] + pub const fn len(&self) -> usize { + N + } + /// Returns an array of the same size as `self`, with function `f` applied to each element /// in order. /// diff --git a/library/core/tests/array.rs b/library/core/tests/array.rs index 0ae625bdb68c6..0fe59c795d8d4 100644 --- a/library/core/tests/array.rs +++ b/library/core/tests/array.rs @@ -288,6 +288,12 @@ fn empty_array_is_always_default() { let _arr = <[DoesNotImplDefault; 0]>::default(); } +#[test] +fn array_len() { + let a = [1, 2, 3]; + assert_eq!(a.len(), 3); +} + #[test] fn array_map() { let a = [1, 2, 3];