File tree 1 file changed +13
-3
lines changed
1 file changed +13
-3
lines changed Original file line number Diff line number Diff line change 4
4
5
5
``` rust,editable
6
6
fn main() {
7
- let mut numbers = Vec::new();
8
- numbers.push(42);
9
-
10
7
let mut v1 = Vec::new();
11
8
v1.push(42);
12
9
println!("v1: len = {}, capacity = {}", v1.len(), v1.capacity());
@@ -15,6 +12,9 @@ fn main() {
15
12
v2.extend(v1.iter());
16
13
v2.push(9999);
17
14
println!("v2: len = {}, capacity = {}", v2.len(), v2.capacity());
15
+
16
+ let mut numbers = vec![1, 2, 3];
17
+ numbers.push(42);
18
18
}
19
19
```
20
20
@@ -23,3 +23,13 @@ methods on a `Vec`.
23
23
24
24
[ 1 ] : https://doc.rust-lang.org/std/vec/struct.Vec.html
25
25
[ 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