Skip to content

Commit 72cf387

Browse files
igor-petrukNoahDragon
authored andcommitted
Update vec.md (google#240)
Adding Speaker Notes about type inference in collections and `vec![]` macro.
1 parent 945393f commit 72cf387

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/std/vec.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
```rust,editable
66
fn main() {
7-
let mut numbers = Vec::new();
8-
numbers.push(42);
9-
107
let mut v1 = Vec::new();
118
v1.push(42);
129
println!("v1: len = {}, capacity = {}", v1.len(), v1.capacity());
@@ -15,6 +12,9 @@ fn main() {
1512
v2.extend(v1.iter());
1613
v2.push(9999);
1714
println!("v2: len = {}, capacity = {}", v2.len(), v2.capacity());
15+
16+
let mut numbers = vec![1, 2, 3];
17+
numbers.push(42);
1818
}
1919
```
2020

@@ -23,3 +23,13 @@ methods on a `Vec`.
2323

2424
[1]: https://doc.rust-lang.org/std/vec/struct.Vec.html
2525
[2]: https://doc.rust-lang.org/std/vec/struct.Vec.html#deref-methods-[T]
26+
27+
<details>
28+
29+
Notice how `Vec<T>` is a generic type too, but you don't have to specify `T` explicitly.
30+
As always with Rust type inference, the `T` was established during the first `push` call.
31+
32+
`vec![...]` is a canonical macro to use instead of `Vec::new()` and it supports
33+
adding initial elements to the vector.
34+
35+
</details>

0 commit comments

Comments
 (0)