1010
1111/*!
1212
13- Utilities for vector manipulation
13+ Utilities for slice manipulation
1414
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.
1817
1918```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"];
2224```
2325
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:
2539
2640## Structs
2741
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 .
3044
3145## Traits
3246
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.
3950
4051An 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)`:
4253
4354```rust
4455let numbers = [0i, 1i, 2i];
4556let last_numbers = numbers.slice(1, 3);
4657// last_numbers is now &[1i, 2i]
4758```
4859
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-
6260## Implementations of other traits
6361
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 :
6664
6765* `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`
7068
7169## Iteration
7270
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`.
7674
7775```rust
7876let numbers = [0i, 1i, 2i];
@@ -82,18 +80,7 @@ for &x in numbers.iter() {
8280```
8381
8482* `.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.
9784
9885*/
9986
0 commit comments