Rust learning: lessons, notes, and exercise crates
This commit is contained in:
@@ -0,0 +1 @@
|
||||
/target
|
||||
Generated
+7
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "learn-collections"
|
||||
version = "0.1.0"
|
||||
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "learn-collections"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
@@ -0,0 +1,76 @@
|
||||
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:?}");
|
||||
}
|
||||
Reference in New Issue
Block a user