Skip to content

Commit 73d3d68

Browse files
committed
TRPL editing: vectors
1 parent a3a2049 commit 73d3d68

File tree

1 file changed

+42
-21
lines changed

1 file changed

+42
-21
lines changed

src/doc/trpl/vectors.md

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,58 @@
11
% Vectors
22

3-
A *vector* is a dynamic or "growable" array, implemented as the standard
4-
library type [`Vec<T>`](../std/vec/) (Where `<T>` is a [Generic](./generics.md)
5-
statement). Vectors always allocate their data on the heap. Vectors are to
6-
[slices][slices] what [`String`][string] is to `&str`. You can
7-
create them with the `vec!` macro:
8-
9-
```{rust}
10-
let v = vec![1, 2, 3]; // v: Vec<i32>
11-
```
3+
A ‘vector’ is a dynamic or ‘growable’ array, implemented as the standard
4+
library type [`Vec<T>`][vec]. That `<T>` is a [generic][generic], meaning we
5+
can have vectors of any type. Vectors always allocate their data on the heap.
6+
You can create them with the `vec!` macro:
127

13-
[slices]: primitive-types.html#slices
14-
[string]: strings.html
8+
```rust
9+
let v = vec![1, 2, 3, 4, 5]; // v: Vec<i32>
10+
```
1511

16-
(Notice that unlike the `println!` macro we've used in the past, we use square
17-
brackets `[]` with `vec!`. Rust allows you to use either in either situation,
12+
(Notice that unlike the `println!` macro weve used in the past, we use square
13+
brackets `[]` with `vec!` macro. Rust allows you to use either in either situation,
1814
this is just convention.)
1915

20-
There's an alternate form of `vec!` for repeating an initial value:
16+
Theres an alternate form of `vec!` for repeating an initial value:
2117

2218
```
2319
let v = vec![0; 10]; // ten zeroes
2420
```
2521

26-
You can get the length of, iterate over, and subscript vectors just like
27-
arrays. In addition, (mutable) vectors can grow automatically:
22+
## Accessing elements
2823

29-
```{rust}
30-
let mut nums = vec![1, 2, 3]; // mut nums: Vec<i32>
24+
To get the value at a particular index in the vector, we use `[]`s:
3125

32-
nums.push(4);
26+
```rust
27+
let v = vec![1, 2, 3, 4, 5];
3328

34-
println!("The length of nums is now {}", nums.len()); // Prints 4
29+
println!("The third element of v is {}", v[2]);
3530
```
3631

37-
Vectors have many more useful methods.
32+
The indices count from `0`, so the third element is `v[2]`.
33+
34+
## Iterating
35+
36+
Once you have a vector, you can iterate through its elements with `for`. There
37+
are three versions:
38+
39+
```rust
40+
let mut v = vec![1, 2, 3, 4, 5];
41+
42+
for i in &v {
43+
println!("A reference to {}", i);
44+
}
45+
46+
for i in &mut v {
47+
println!("A mutable reference to {}", i);
48+
}
49+
50+
for i in v {
51+
println!("Take ownership of the vector and its element {}", i);
52+
}
53+
```
54+
55+
Vectors have many more useful methods, which you can read about in [their
56+
API documentation][vec].
57+
58+
[vec]: ../std/vec/index.html

0 commit comments

Comments
 (0)