Files
learn-rust/learn-collections/src/main.rs
T

77 lines
1.6 KiB
Rust

enum SpreadsheetCell {
Int(i32),
Float(f32),
Text(String),
}
fn main() {
println!("Hello, world!");
let mut v = vec![1, 2, 3, 4];
v.push(5);
v.push(6);
v.push(7);
let Some(&x) = v.get(3) else {
println!("index not found");
return;
};
for i in &v {
print!("{i}");
}
println!("");
for i in &mut v {
*i = *i * 10;
}
for i in &v {
print!("{i}");
}
println!("");
println!("this is the value of the 100th index {x}");
let _row = vec![
SpreadsheetCell::Int(3),
SpreadsheetCell::Float(3.14),
SpreadsheetCell::Text("bodo".into()),
];
let _s1 = String::from("Hello");
let _s2 = "World".to_string();
let _s1: String = _s1 + &_s2;
println!("THis is: {_s1}");
let _s3 = "FooBar".to_string();
let _s = format!("{_s1} {_s3}");
println!("{}", _s);
use std::collections::HashMap;
let mut scores = HashMap::new();
scores.insert("blue".to_string(), 1);
scores.insert("red".to_string(), 3);
let score_blue = scores.get(&"blue".to_string()).copied().unwrap_or(0);
let score_red = scores.get(&"red".to_string()).copied().unwrap_or(0);
println!("this is blue: {}, this is red {}", score_blue, score_red);
for (k, v) in &scores {
println!("team {k}: {v} scores");
}
scores.insert("green".to_string(), 10);
scores.insert(String::from("Blue"), 10);
scores.insert(String::from("Blue"), 25);
println!("{scores:?}");
scores.entry("blue".to_string()).or_insert(100);
scores.entry("yellow".to_string()).or_insert(100);
println!("{scores:?}");
}