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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
fn main() {
// Vector
let v: Vec<i32> = Vec::new();
// Macro
let v = vec![1, 2, 3];
// Update
let mut v = Vec::new();
v.push(5);
v.push(6);
v.push(7);
v.push(8);
// Free when out of scope
{
let v = vec![1, 2, 3];
}
// Access
let mut v = vec![1, 2, 3, 4, 5];
let third: &i32 = &v[2];
println!("The third element is {}", third);
match v.get(2) {
Some(third) => println!("The third element is {}", third),
None => println!("There is no third element."),
}
// immutable borrow
// let first = &v[0];
let first = v[0];
v.push(6);
println!("The first element is: {}", first);
// Iterating
let v = vec![100, 32, 57];
for i in &v {
println!("{}", i);
}
// Iterating(mutable)
let mut v = vec![100, 32, 57];
for i in &mut v {
*i += 50;
}
// Storing multiple types using enum
#[derive(Debug)]
enum SpreadSheetCell {
Int(i32),
Float(f64),
Text(String),
}
let row = vec![
SpreadSheetCell::Int(3),
SpreadSheetCell::Text(String::from("blue")),
SpreadSheetCell::Float(10.12),
];
for (i, cell) in row.iter().enumerate() {
println!("Cell {} is {:?}", i, cell);
}
// Push string
let mut s = String::from("foo");
s.push_str("bar");
let mut s = String::from("ol");
s.push('l');
// Concatenation
let s1 = String::from("tic");
let s2 = String::from("tac");
let s3 = String::from("toe");
// take owership of s1, append s2 and s3 to it
let s = s1 + "-" + &s2 + "-" + &s3;
println!("{}", s);
let s1 = String::from("tic");
let s2 = String::from("tac");
let s3 = String::from("toe");
let s = format!("{}-{}-{}", s1, s2, s3);
println!("{}", s);
// Iterating
for char in "你好世界".chars() {
println!("{}", char);
}
for byte in "你好世界".bytes() {
println!("{}", byte);
}
// Hash map
use std::collections::HashMap;
let mut scores = HashMap::new();
scores.insert(String::from("Blue"), 10);
scores.insert(String::from("Yellow"), 50);
// Using `collect` method
let teams = vec![String::from("Blue"), String::from("Yellow")];
let initial_scores = vec![10, 50];
let mut scores: HashMap<_, _> = teams.into_iter().zip(initial_scores.into_iter()).collect();
// Owership
let field_name = String::from("Favorite color");
let field_value = String::from("Blue");
let mut map = HashMap::new();
map.insert(field_name, field_value);
// field_name and field_value is invaid now
// println!("field_name: {}, field_value: {}", field_name, field_value);
// Access
let team_name = String::from("Blue");
let score = scores.get(&team_name);
match score {
Some(score) => println!("{}: {}", team_name, score),
None => println!("{} not found", team_name),
}
// if let Some(score) = score {
// println!("{}: {}", team_name, score);
// } else {
// println!("{} not found", team_name);
// }
for (key, value) in &scores {
println!("{}: {}", key, value);
}
// Updating(overwriting)
scores.insert(String::from("Blue"), 25);
println!("{:?}", scores);
// Updating(insert if not exist)
scores.entry(String::from("Red")).or_insert(50);
scores.entry(String::from("Blue")).or_insert(50);
println!("{:?}", scores);
// Updating(based on old value)
let text = "hello world wonderful world";
let mut map = HashMap::new();
for word in text.split_whitespace() {
let count = map.entry(word).or_insert(0);
*count += 1;
}
println!("{:?}", map);
}
|