10
10
11
11
/*!
12
12
13
- The `vec` module contains useful code to help work with vector values. Vectors are Rust's list
14
- type. Vectors contain zero or more values of homogeneous types:
13
+ The `vec` module contains useful code to help work with vector values.
14
+ Vectors are Rust's list type. Vectors contain zero or more values of
15
+ homogeneous types:
15
16
16
17
~~~ {.rust}
17
18
let int_vector = [1,2,3];
@@ -27,32 +28,72 @@ represents iteration over a vector.
27
28
28
29
## Traits
29
30
30
- A number of traits that allow you to accomplish tasks with vectors, like the
31
- `MutableVector` and `ImmutableVector` traits.
31
+ A number of traits add methods that allow you to accomplish tasks with vectors.
32
+
33
+ Traits defined for the `&[T]` type (a vector slice), have methods that can be
34
+ called on either owned vectors, denoted `~[T]`, or on vector slices themselves.
35
+ These traits include `ImmutableVector`, and `MutableVector` for the `&mut [T]`
36
+ case.
37
+
38
+ An example is the method `.slice(a, b)` that returns an immutable "view" into
39
+ a vector or a vector slice from the index interval `[a, b)`:
40
+
41
+ ~~~ {.rust}
42
+ let numbers = [0, 1, 2];
43
+ let last_numbers = numbers.slice(1, 3);
44
+ // last_numbers is now &[1, 2]
45
+ ~~~
46
+
47
+ Traits defined for the `~[T]` type, like `OwnedVector`, can only be called
48
+ on such vectors. These methods deal with adding elements or otherwise changing
49
+ the allocation of the vector.
50
+
51
+ An example is the method `.push(element)` that will add an element at the end
52
+ of the vector:
53
+
54
+ ~~~ {.rust}
55
+ let mut numbers = ~[0, 1, 2];
56
+ numbers.push(7);
57
+ // numbers is now ~[0, 1, 2, 7];
58
+ ~~~
32
59
33
60
## Implementations of other traits
34
61
35
- Vectors are a very useful type, and so there's tons of implementations of
36
- traits found elsewhere . Some notable examples:
62
+ Vectors are a very useful type, and so there's several implementations of
63
+ traits from other modules . Some notable examples:
37
64
38
65
* `Clone`
39
- * `Iterator`
40
- * `Zero`
66
+ * `Eq`, `Ord`, `TotalEq`, `TotalOrd` -- vectors can be compared,
67
+ if the element type defines the corresponding trait.
41
68
42
- ## Function definitions
69
+ ## Iteration
70
+
71
+ The method `iter()` returns an iteration value for a vector or a vector slice.
72
+ The iterator yields borrowed pointers to the vector's elements, so if the element
73
+ type of the vector is `int`, the element type of the iterator is `&int`.
43
74
44
- There are a number of different functions that take vectors, here are some
45
- broad categories:
75
+ ~~~ {.rust}
76
+ let numbers = [0, 1, 2];
77
+ for &x in numbers.iter() {
78
+ println!("{} is a number!", x);
79
+ }
80
+ ~~~
81
+
82
+ * `.rev_iter()` returns an iterator with the same values as `.iter()`,
83
+ but going in the reverse order, starting with the back element.
84
+ * `.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
46
90
47
- * Modifying a vector, like `append` and `grow`.
48
- * Searching in a vector, like `bsearch`.
49
- * Iterating over vectors, like `each_permutation`.
50
- * Functional transformations on vectors, like `map` and `partition`.
51
- * Stack/queue operations, like `push`/`pop` and `shift`/`unshift`.
52
- * Cons-y operations, like `head` and `tail`.
53
- * Zipper operations, like `zip` and `unzip`.
91
+ There are a number of free functions that create or take vectors, for example:
54
92
55
- And much, much more.
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`.
56
97
57
98
*/
58
99
0 commit comments