|
13 | 13 | //! The `slice` module contains useful code to help work with slice values.
|
14 | 14 | //! Slices are a view into a block of memory represented as a pointer and a length.
|
15 | 15 | //!
|
16 |
| -//! ```rust |
17 |
| -//! # #![feature(core)] |
| 16 | +//! ``` |
18 | 17 | //! // slicing a Vec
|
19 |
| -//! let vec = vec!(1, 2, 3); |
20 |
| -//! let int_slice = vec.as_slice(); |
| 18 | +//! let vec = vec![1, 2, 3]; |
| 19 | +//! let int_slice = &vec[..]; |
21 | 20 | //! // coercing an array to a slice
|
22 | 21 | //! let str_slice: &[&str] = &["one", "two", "three"];
|
23 | 22 | //! ```
|
24 | 23 | //!
|
25 | 24 | //! Slices are either mutable or shared. The shared slice type is `&[T]`,
|
26 |
| -//! while the mutable slice type is `&mut[T]`. For example, you can mutate the |
27 |
| -//! block of memory that a mutable slice points to: |
| 25 | +//! while the mutable slice type is `&mut [T]`, where `T` represents the element |
| 26 | +//! type. For example, you can mutate the block of memory that a mutable slice |
| 27 | +//! points to: |
28 | 28 | //!
|
29 |
| -//! ```rust |
30 |
| -//! let x: &mut[i32] = &mut [1, 2, 3]; |
| 29 | +//! ``` |
| 30 | +//! let x = &mut [1, 2, 3]; |
31 | 31 | //! x[1] = 7;
|
32 |
| -//! assert_eq!(x[0], 1); |
33 |
| -//! assert_eq!(x[1], 7); |
34 |
| -//! assert_eq!(x[2], 3); |
| 32 | +//! assert_eq!(x, &[1, 7, 3]); |
35 | 33 | //! ```
|
36 | 34 | //!
|
37 | 35 | //! Here are some of the things this module contains:
|
|
41 | 39 | //! There are several structs that are useful for slices, such as `Iter`, which
|
42 | 40 | //! represents iteration over a slice.
|
43 | 41 | //!
|
44 |
| -//! ## Traits |
45 |
| -//! |
46 |
| -//! A number of traits add methods that allow you to accomplish tasks |
47 |
| -//! with slices, the most important being `SliceExt`. Other traits |
48 |
| -//! apply only to slices of elements satisfying certain bounds (like |
49 |
| -//! `Ord`). |
50 |
| -//! |
51 |
| -//! An example is the `slice` method which enables slicing syntax `[a..b]` that |
52 |
| -//! returns an immutable "view" into a `Vec` or another slice from the index |
53 |
| -//! interval `[a, b)`: |
54 |
| -//! |
55 |
| -//! ```rust |
56 |
| -//! fn main() { |
57 |
| -//! let numbers = [0, 1, 2]; |
58 |
| -//! let last_numbers = &numbers[1..3]; |
59 |
| -//! // last_numbers is now &[1, 2] |
60 |
| -//! } |
61 |
| -//! ``` |
62 |
| -//! |
63 |
| -//! ## Implementations of other traits |
| 42 | +//! ## Trait Implementations |
64 | 43 | //!
|
65 | 44 | //! There are several implementations of common traits for slices. Some examples
|
66 | 45 | //! include:
|
67 | 46 | //!
|
68 | 47 | //! * `Clone`
|
69 |
| -//! * `Eq`, `Ord` - for immutable slices whose element type are `Eq` or `Ord`. |
| 48 | +//! * `Eq`, `Ord` - for slices whose element type are `Eq` or `Ord`. |
70 | 49 | //! * `Hash` - for slices whose element type is `Hash`
|
71 | 50 | //!
|
72 | 51 | //! ## Iteration
|
73 | 52 | //!
|
74 |
| -//! The method `iter()` returns an iteration value for a slice. The iterator |
75 |
| -//! yields references to the slice's elements, so if the element |
76 |
| -//! type of the slice is `isize`, the element type of the iterator is `&isize`. |
| 53 | +//! The slices implement `IntoIterator`. The iterators of yield references |
| 54 | +//! to the slice elements. |
77 | 55 | //!
|
78 |
| -//! ```rust |
79 |
| -//! let numbers = [0, 1, 2]; |
80 |
| -//! for &x in numbers.iter() { |
81 |
| -//! println!("{} is a number!", x); |
| 56 | +//! ``` |
| 57 | +//! let numbers = &[0, 1, 2]; |
| 58 | +//! for n in numbers { |
| 59 | +//! println!("{} is a number!", n); |
82 | 60 | //! }
|
83 | 61 | //! ```
|
84 | 62 | //!
|
85 |
| -//! * `.iter_mut()` returns an iterator that allows modifying each value. |
86 |
| -//! * Further iterators exist that split, chunk or permute the slice. |
| 63 | +//! The mutable slice yields mutable references to the elements: |
| 64 | +//! |
| 65 | +//! ``` |
| 66 | +//! let mut scores = [7, 8, 9]; |
| 67 | +//! for score in &mut scores[..] { |
| 68 | +//! *score += 1; |
| 69 | +//! } |
| 70 | +//! ``` |
| 71 | +//! |
| 72 | +//! This iterator yields mutable references to the slice's elements, so while the element |
| 73 | +//! type of the slice is `i32`, the element type of the iterator is `&mut i32`. |
| 74 | +//! |
| 75 | +//! * `.iter()` and `.iter_mut()` are the explicit methods to return the default |
| 76 | +//! iterators. |
| 77 | +//! * Further methods that return iterators are `.split()`, `.splitn()`, |
| 78 | +//! `.chunks()`, `.windows()` and more. |
87 | 79 |
|
88 | 80 | #![doc(primitive = "slice")]
|
89 | 81 | #![stable(feature = "rust1", since = "1.0.0")]
|
|
0 commit comments