|
1 |
| -//! A dynamically-sized view into a contiguous sequence, `[T]`. |
| 1 | +//! Utilities for the slice primitive type. |
2 | 2 | //!
|
3 | 3 | //! *[See also the slice primitive type](slice).*
|
4 | 4 | //!
|
5 |
| -//! Slices are a view into a block of memory represented as a pointer and a |
6 |
| -//! length. |
| 5 | +//! Most of the structs in this module are iterator types which can only be created |
| 6 | +//! using a certain function. For example, `slice.iter()` yields an [`Iter`]. |
7 | 7 | //!
|
8 |
| -//! ``` |
9 |
| -//! // slicing a Vec |
10 |
| -//! let vec = vec![1, 2, 3]; |
11 |
| -//! let int_slice = &vec[..]; |
12 |
| -//! // coercing an array to a slice |
13 |
| -//! let str_slice: &[&str] = &["one", "two", "three"]; |
14 |
| -//! ``` |
15 |
| -//! |
16 |
| -//! Slices are either mutable or shared. The shared slice type is `&[T]`, |
17 |
| -//! while the mutable slice type is `&mut [T]`, where `T` represents the element |
18 |
| -//! type. For example, you can mutate the block of memory that a mutable slice |
19 |
| -//! points to: |
20 |
| -//! |
21 |
| -//! ``` |
22 |
| -//! let x = &mut [1, 2, 3]; |
23 |
| -//! x[1] = 7; |
24 |
| -//! assert_eq!(x, &[1, 7, 3]); |
25 |
| -//! ``` |
26 |
| -//! |
27 |
| -//! Here are some of the things this module contains: |
28 |
| -//! |
29 |
| -//! ## Structs |
30 |
| -//! |
31 |
| -//! There are several structs that are useful for slices, such as [`Iter`], which |
32 |
| -//! represents iteration over a slice. |
33 |
| -//! |
34 |
| -//! ## Trait Implementations |
35 |
| -//! |
36 |
| -//! There are several implementations of common traits for slices. Some examples |
37 |
| -//! include: |
38 |
| -//! |
39 |
| -//! * [`Clone`] |
40 |
| -//! * [`Eq`], [`Ord`] - for slices whose element type are [`Eq`] or [`Ord`]. |
41 |
| -//! * [`Hash`] - for slices whose element type is [`Hash`]. |
42 |
| -//! |
43 |
| -//! ## Iteration |
44 |
| -//! |
45 |
| -//! The slices implement `IntoIterator`. The iterator yields references to the |
46 |
| -//! slice elements. |
47 |
| -//! |
48 |
| -//! ``` |
49 |
| -//! let numbers = &[0, 1, 2]; |
50 |
| -//! for n in numbers { |
51 |
| -//! println!("{n} is a number!"); |
52 |
| -//! } |
53 |
| -//! ``` |
54 |
| -//! |
55 |
| -//! The mutable slice yields mutable references to the elements: |
56 |
| -//! |
57 |
| -//! ``` |
58 |
| -//! let mut scores = [7, 8, 9]; |
59 |
| -//! for score in &mut scores[..] { |
60 |
| -//! *score += 1; |
61 |
| -//! } |
62 |
| -//! ``` |
63 |
| -//! |
64 |
| -//! This iterator yields mutable references to the slice's elements, so while |
65 |
| -//! the element type of the slice is `i32`, the element type of the iterator is |
66 |
| -//! `&mut i32`. |
67 |
| -//! |
68 |
| -//! * [`.iter`] and [`.iter_mut`] are the explicit methods to return the default |
69 |
| -//! iterators. |
70 |
| -//! * Further methods that return iterators are [`.split`], [`.splitn`], |
71 |
| -//! [`.chunks`], [`.windows`] and more. |
72 |
| -//! |
73 |
| -//! [`Hash`]: core::hash::Hash |
74 |
| -//! [`.iter`]: slice::iter |
75 |
| -//! [`.iter_mut`]: slice::iter_mut |
76 |
| -//! [`.split`]: slice::split |
77 |
| -//! [`.splitn`]: slice::splitn |
78 |
| -//! [`.chunks`]: slice::chunks |
79 |
| -//! [`.windows`]: slice::windows |
| 8 | +//! A few functions are provided to create a slice from a value reference |
| 9 | +//! or from a raw pointer. |
80 | 10 | #![stable(feature = "rust1", since = "1.0.0")]
|
81 | 11 | // Many of the usings in this module are only used in the test configuration.
|
82 | 12 | // It's cleaner to just turn off the unused_imports warning than to fix them.
|
|
0 commit comments