summaryrefslogtreecommitdiff
path: root/collections/src
diff options
context:
space:
mode:
authorJordan Gong <jordan.gong@protonmail.com>2020-08-31 17:17:31 +0800
committerJordan Gong <jordan.gong@protonmail.com>2020-08-31 17:17:31 +0800
commit79833af30e82bc258fdf300d5c6701e39ffc95da (patch)
treed282dc1d8504123f5dbcb4b52904ad7f6108b30b /collections/src
parent3cda6fa25939e2857f5de116af976b79179cbcf8 (diff)
Store data in different way
Diffstat (limited to 'collections/src')
-rw-r--r--collections/src/main.rs167
1 files changed, 167 insertions, 0 deletions
diff --git a/collections/src/main.rs b/collections/src/main.rs
new file mode 100644
index 0000000..0930ce9
--- /dev/null
+++ b/collections/src/main.rs
@@ -0,0 +1,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);
+}