From bd91374d46faa0ccf4ed5cce4c5556322e7c9f1f Mon Sep 17 00:00:00 2001 From: Reality Enjoyer Date: Tue, 13 Jan 2026 08:32:20 +0000 Subject: [PATCH] strings --- hash_maps/Cargo.toml | 6 +++++ hash_maps/src/main.rs | 31 ++++++++++++++++++++++++++ strings/Cargo.toml | 6 +++++ strings/src/main.rs | 52 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+) create mode 100644 hash_maps/Cargo.toml create mode 100644 hash_maps/src/main.rs create mode 100644 strings/Cargo.toml create mode 100644 strings/src/main.rs diff --git a/hash_maps/Cargo.toml b/hash_maps/Cargo.toml new file mode 100644 index 0000000..bad93be --- /dev/null +++ b/hash_maps/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hash_maps" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/hash_maps/src/main.rs b/hash_maps/src/main.rs new file mode 100644 index 0000000..d0070a5 --- /dev/null +++ b/hash_maps/src/main.rs @@ -0,0 +1,31 @@ +use std::collections::HashMap; + +fn main() { + use std::collections::HashMap; + + let mut capitals = HashMap::new(); + + capitals.insert(String::from("Tokyo"), 43_000_000); + capitals.insert(String::from("Beijing"), 62_000_000); + capitals.insert(String::from("South Korea"), 29_000_000); + + for (key, value) in &capitals { + println!("{key}: {value}") + } + + let test1 = String::from("Tokyo"); + let test2 = String::from("London"); + + let passing_test = get_population(capitals.clone(), test1); + let failing_test = get_population(capitals, test2); + + println!( + "\npassing test: {:?}\nfailing test: {:?}", + passing_test, failing_test + ); +} + +fn get_population(capitals: HashMap, key: String) -> u32 { + let population = capitals.get(&key).copied().unwrap_or(0); + population +} diff --git a/strings/Cargo.toml b/strings/Cargo.toml new file mode 100644 index 0000000..5e6e691 --- /dev/null +++ b/strings/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "strings" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/strings/src/main.rs b/strings/src/main.rs new file mode 100644 index 0000000..38d058d --- /dev/null +++ b/strings/src/main.rs @@ -0,0 +1,52 @@ +fn main() { + // strings: collections of bytes with some useful methods + + // akshually, Rust only has ONE string type in the core language + // the string slice 'str' + // string literals = stored in the binary + // string slices = reference to string data stored elsewhere + // it's all UTF-8 encoded under the hood + + let s = "hello darkness".to_string(); + + let mut half = "my old ".to_string(); + + //using push_str() to append to a string + half.push_str("friend!"); + + // you can add strings using +... + + let space = " ".to_string(); + + // let final_string = s + " " + ½ + // let final_string = s + &space + ½ + // println!("{}", final_string); + + // ...or using format!() + + let final_formatted_string = format!("{s}{space}{half}"); + println!("{}", final_formatted_string); +} + +// let hello = String::from("السلام عليكم"); +// println!("{}", hello); +// let hello = String::from("Dobrý den"); +// println!("{}", hello); +// let hello = String::from("Hello"); +// println!("{}", hello); +// let hello = String::from("שלום"); +// println!("{}", hello); +// let hello = String::from("नमस्ते"); +// println!("{}", hello); +// let hello = String::from("こんにちは"); +// println!("{}", hello); +// let hello = String::from("안녕하세요"); +// println!("{}", hello); +// let hello = String::from("你好"); +// println!("{}", hello); +// let hello = String::from("Olá"); +// println!("{}", hello); +// let hello = String::from("Здравствуйте"); +// println!("{}", hello); +// let hello = String::from("Hola"); +// println!("{}", hello);