File tree Expand file tree Collapse file tree 1 file changed +13
-3
lines changed Expand file tree Collapse file tree 1 file changed +13
-3
lines changed Original file line number Diff line number Diff line change 44
55``` rust,editable
66fn 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 >
You can’t perform that action at this time.
0 commit comments