Skip to content

Commit 77dff93

Browse files
author
blake2-ppc
committed
std::vec: Update module doc text
Update for a lot of changes (not many free functions left), add examples of the important methods `slice` and `push`, and write a short bit about iteration.
1 parent de9546a commit 77dff93

File tree

1 file changed

+60
-19
lines changed

1 file changed

+60
-19
lines changed

src/libstd/vec.rs

+60-19
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010

1111
/*!
1212
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:
1516
1617
~~~ {.rust}
1718
let int_vector = [1,2,3];
@@ -27,32 +28,72 @@ represents iteration over a vector.
2728
2829
## Traits
2930
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+
~~~
3259
3360
## Implementations of other traits
3461
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:
3764
3865
* `Clone`
39-
* `Iterator`
40-
* `Zero`
66+
* `Eq`, `Ord`, `TotalEq`, `TotalOrd` -- vectors can be compared,
67+
if the element type defines the corresponding trait.
4168
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`.
4374
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
4690
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:
5492
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`.
5697
5798
*/
5899

0 commit comments

Comments
 (0)