From 79833af30e82bc258fdf300d5c6701e39ffc95da Mon Sep 17 00:00:00 2001 From: Jordan Gong Date: Mon, 31 Aug 2020 17:17:31 +0800 Subject: Store data in different way --- collections/src/main.rs | 167 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 collections/src/main.rs (limited to 'collections/src/main.rs') 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 = 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); +} -- cgit v1.2.3