gw_rust_programming_tutorial/chapter_8/
test_collection.rs1pub fn test_vec() {
2 let v: Vec<i32> = Vec::new();
3
4 let v = vec![1, 2, 3];
6
7 let mut v = Vec::new();
8
9 v.push(5);
10 v.push(6);
11 v.push(7);
12 v.push(8);
13
14 println!("vec={:?}", v);
15
16 let v = vec![1, 2, 3, 4, 5];
17
18 let third: &i32 = &v[2];
19 println!("The third element is {}", third);
20
21 match v.get(2) {
22 Some(third) => println!("The third element is {}", third),
23 None => println!("There is no third element."),
24 }
25
26 let mut v = vec![100, 32, 57];
28 for i in &mut v {
29 *i += 50;
30 }
31 println!("遍历赋值后 vec={:?}", v);
32
33 #[derive(Debug)]
35 enum SpreadsheetCell {
36 Int(i32),
37 Float(f64),
38 Text(String),
39 }
40
41 let row = vec![
42 SpreadsheetCell::Int(3),
43 SpreadsheetCell::Text(String::from("blue")),
44 SpreadsheetCell::Float(10.12),
45 ];
46
47 println!("enum row={:?}", row);
48}