10
10
11
11
/*!
12
12
13
- Utilities for vector manipulation
13
+ Utilities for slice manipulation
14
14
15
- The `vec` module contains useful code to help work with vector values.
16
- Vectors are Rust's list type. Vectors contain zero or more values of
17
- homogeneous types:
15
+ The `slice` module contains useful code to help work with slice values.
16
+ Slices are a view into a block of memory represented as a pointer and a length.
18
17
19
18
```rust
20
- let int_vector = [1i, 2i, 3i];
21
- let str_vector = ["one", "two", "three"];
19
+ // slicing a Vec
20
+ let vec = vec!(1i, 2, 3);
21
+ let int_slice = vec.as_slice();
22
+ // coercing an array to a slice
23
+ let str_slice: &[&str] = ["one", "two", "three"];
22
24
```
23
25
24
- This is a big module, but for a high-level overview:
26
+ Slices are either mutable or shared. The shared slice type is `&[T]`,
27
+ while the mutable slice type is `&mut[T]`. For example, you can mutate the
28
+ block of memory that a mutable slice points to:
29
+
30
+ ```rust
31
+ let x: &mut[int] = [1i, 2, 3];
32
+ x[1] = 7;
33
+ assert_eq!(x[0], 1);
34
+ assert_eq!(x[1], 7);
35
+ assert_eq!(x[2], 3);
36
+ ```
37
+
38
+ Here are some of the things this module contains:
25
39
26
40
## Structs
27
41
28
- Several structs that are useful for vectors , such as `Items`, which
29
- represents iteration over a vector .
42
+ There are several structs that are useful for slices , such as `Items`, which
43
+ represents iteration over a slice .
30
44
31
45
## Traits
32
46
33
- A number of traits add methods that allow you to accomplish tasks with vectors.
34
-
35
- Traits defined for the `&[T]` type (a vector slice), have methods that can be
36
- called on either owned vectors, denoted `~[T]`, or on vector slices themselves.
37
- These traits include `ImmutableVector`, and `MutableVector` for the `&mut [T]`
38
- case.
47
+ A number of traits add methods that allow you to accomplish tasks with slices.
48
+ These traits include `ImmutableVector`, which is defined for `&[T]` types,
49
+ and `MutableVector`, defined for `&mut [T]` types.
39
50
40
51
An example is the method `.slice(a, b)` that returns an immutable "view" into
41
- a vector or a vector slice from the index interval `[a, b)`:
52
+ a `Vec` or another slice from the index interval `[a, b)`:
42
53
43
54
```rust
44
55
let numbers = [0i, 1i, 2i];
45
56
let last_numbers = numbers.slice(1, 3);
46
57
// last_numbers is now &[1i, 2i]
47
58
```
48
59
49
- Traits defined for the `~[T]` type, like `OwnedVector`, can only be called
50
- on such vectors. These methods deal with adding elements or otherwise changing
51
- the allocation of the vector.
52
-
53
- An example is the method `.push(element)` that will add an element at the end
54
- of the vector:
55
-
56
- ```rust
57
- let mut numbers = vec![0i, 1i, 2i];
58
- numbers.push(7);
59
- // numbers is now vec![0i, 1i, 2i, 7i];
60
- ```
61
-
62
60
## Implementations of other traits
63
61
64
- Vectors are a very useful type, and so there's several implementations of
65
- traits from other modules. Some notable examples :
62
+ There are several implementations of common traits for slices. Some examples
63
+ include :
66
64
67
65
* `Clone`
68
- * `Eq`, `Ord`, `Eq`, `Ord` -- vectors can be compared,
69
- if the element type defines the corresponding trait.
66
+ * `Eq`, `Ord` - for immutable slices whose element type are `Eq` or `Ord`.
67
+ * `Hash` - for slices whose element type is `Hash`
70
68
71
69
## Iteration
72
70
73
- The method `iter()` returns an iteration value for a vector or a vector slice.
74
- The iterator yields references to the vector 's elements, so if the element
75
- type of the vector is `int`, the element type of the iterator is `&int`.
71
+ The method `iter()` returns an iteration value for a slice. The iterator
72
+ yields references to the slice 's elements, so if the element
73
+ type of the slice is `int`, the element type of the iterator is `&int`.
76
74
77
75
```rust
78
76
let numbers = [0i, 1i, 2i];
@@ -82,18 +80,7 @@ for &x in numbers.iter() {
82
80
```
83
81
84
82
* `.mut_iter()` returns an iterator that allows modifying each value.
85
- * `.move_iter()` converts an owned vector into an iterator that
86
- moves out a value from the vector each iteration.
87
- * Further iterators exist that split, chunk or permute the vector.
88
-
89
- ## Function definitions
90
-
91
- There are a number of free functions that create or take vectors, for example:
92
-
93
- * Creating a vector, like `from_elem` and `from_fn`
94
- * Creating a vector with a given size: `with_capacity`
95
- * Modifying a vector and returning it, like `append`
96
- * Operations on paired elements, like `unzip`.
83
+ * Further iterators exist that split, chunk or permute the slice.
97
84
98
85
*/
99
86
0 commit comments