rust_learning/collections/src/main.rs

64 lines
1.4 KiB
Rust

use std::collections::HashMap;
fn main() {
let mut v = vec![1, 2, 3, 4, 5];
let first = &v[0];
println!("The first element is: {}", first);
v.push(6);
let s1 = String::from("Hello, ");
let s2 = String::from("world!");
let s3 = s1 + &s2;
println!("{}", s3);
let s1 = String::from("tic");
let s2 = String::from("tac");
let s3 = String::from("toe");
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);
for c in "नमस्ते".chars() {
println!("{}", c);
}
let mut scores = HashMap::new();
scores.insert(String::from("Blue"), 10);
scores.entry(String::from("Blue")).or_insert(50);
scores.entry(String::from("Yellow")).or_insert(50);
let team_name = String::from("Blue");
let score = scores.get(&team_name);
if let Some(i) = score {
println!("{} team score is {}", team_name, i);
} else {
println!("{} team not found", team_name);
}
for (key, value) in &scores {
println!("{}: {}", key, 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);
}