-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvec.rs
44 lines (36 loc) · 865 Bytes
/
vec.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#![allow(unused)]
// Vector
fn main() {
// Vec<T>
let v: Vec<i32> = vec![1, 2, 3];
// Vec<u8>
let v = vec![1u8, 2, 3, 4];
println!("vec = {:?}, length = {}", v, v.len());
// Get
let x = v[1];
println!("{}", x);
let x = v.get(1);
match x {
Some(val) => println!("get: {:?}", val),
None => println!("get: value doesn't exist"),
}
// Update
let mut v = vec![1, 2, 3];
v[1] = 10;
// push - append to vector
let mut v = Vec::new();
v.push(5);
v.push(6);
v.push(7);
println!("vec = {:?}", v);
// pop - remove last element
let mut v = vec![1, 2];
match v.pop() {
Some(val) => println!("pop: {val}"),
None => println!("pop: none"),
}
// Slice
let v = vec![1, 2, 3, 4, 5];
let s = &v[1..3];
println!("slice = {:?}", s);
}