|
8 | 8 | // option. This file may not be copied, modified, or distributed
|
9 | 9 | // except according to those terms.
|
10 | 10 |
|
11 |
| -//! The fixed-size array type (`[T; n]`). |
| 11 | +//! A fixed-size array is denoted `[T; N]` for the element type `T` and |
| 12 | +//! the compile time constant size `N`. The size should be zero or positive. |
12 | 13 | //!
|
13 |
| -//! Some usage examples: |
| 14 | +//! Arrays values are created either with an explicit expression that lists |
| 15 | +//! each element: `[x, y, z]` or a repeat expression: `[x; N]`. The repeat |
| 16 | +//! expression requires that the element type is `Copy`. |
| 17 | +//! |
| 18 | +//! The type `[T; N]` is `Copy` if `T: Copy`. |
| 19 | +//! |
| 20 | +//! Arrays of sizes from 0 to 32 (inclusive) implement the following traits |
| 21 | +//! if the element type allows it: |
| 22 | +//! |
| 23 | +//! - `Clone` |
| 24 | +//! - `Debug` |
| 25 | +//! - `IntoIterator` (implemented for `&[T; N]` and `&mut [T; N]`) |
| 26 | +//! - `PartialEq`, `PartialOrd`, `Ord`, `Eq` |
| 27 | +//! - `Hash` |
| 28 | +//! - `AsRef`, `AsMut` |
| 29 | +//! |
| 30 | +//! Arrays dereference to [slices (`[T]`)][slice], so their methods can be called |
| 31 | +//! on arrays. |
| 32 | +//! |
| 33 | +//! [slice]: primitive.slice.html |
| 34 | +//! |
| 35 | +//! ## Examples |
14 | 36 | //!
|
15 | 37 | //! ```
|
16 |
| -//! let array: [i32; 3] = [0, 1, 2]; |
| 38 | +//! let mut array: [i32; 3] = [0; 3]; |
| 39 | +//! |
| 40 | +//! array[1] = 1; |
| 41 | +//! array[2] = 2; |
17 | 42 | //!
|
18 |
| -//! assert_eq!(0, array[0]); |
19 |
| -//! assert_eq!([0, 1], &array[..2]); |
| 43 | +//! assert_eq!([1, 2], &array[1..]); |
20 | 44 | //!
|
| 45 | +//! // This loop prints: 0 1 2 |
21 | 46 | //! for x in &array {
|
22 |
| -//! println!("{}", x); |
| 47 | +//! print!("{} ", x); |
23 | 48 | //! }
|
| 49 | +//! |
24 | 50 | //! ```
|
| 51 | +//! |
| 52 | +//! Rust does not currently support generics over the size of an array type. |
| 53 | +//! |
25 | 54 |
|
26 | 55 | #![doc(primitive = "array")]
|
0 commit comments