You've already forked book-exercises
strings
This commit is contained in:
6
hash_maps/Cargo.toml
Normal file
6
hash_maps/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "hash_maps"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
31
hash_maps/src/main.rs
Normal file
31
hash_maps/src/main.rs
Normal file
@@ -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<String, u32>, key: String) -> u32 {
|
||||
let population = capitals.get(&key).copied().unwrap_or(0);
|
||||
population
|
||||
}
|
||||
6
strings/Cargo.toml
Normal file
6
strings/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "strings"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
52
strings/src/main.rs
Normal file
52
strings/src/main.rs
Normal file
@@ -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);
|
||||
Reference in New Issue
Block a user