Skip to content

Add separate array length function #86404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions library/core/src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, const N: usize> [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")]
Comment on lines +402 to +403
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the #[stable] feature and version should be the same as the one for #[rustc_const_stable]?

#[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.
///
Expand Down
6 changes: 6 additions & 0 deletions library/core/tests/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down