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); }