progress(modules + hashmaps): finished modules and almost done hashmaps

This commit is contained in:
2026-07-26 02:09:37 +02:00
parent f0f6a72101
commit 2844976587
6 changed files with 12 additions and 53 deletions
-49
View File
@@ -1,49 +0,0 @@
#[derive(Debug)]
struct Order {
name: String,
year: u32,
made_by_phone: bool,
made_by_mobile: bool,
made_by_email: bool,
item_number: u32,
count: u32,
}
fn create_order_template() -> Order {
Order {
name: String::from("Bob"),
year: 2019,
made_by_phone: false,
made_by_mobile: false,
made_by_email: true,
item_number: 123,
count: 0,
}
}
fn main() {
// You can optionally experiment here.
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn your_order() {
let order_template = create_order_template();
// TODO: Create your own order using the update syntax and template above!
// let your_order =
let your_order = ;
assert_eq!(your_order.name, "Hacker in Rust");
assert_eq!(your_order.year, order_template.year);
assert_eq!(your_order.made_by_phone, order_template.made_by_phone);
assert_eq!(your_order.made_by_mobile, order_template.made_by_mobile);
assert_eq!(your_order.made_by_email, order_template.made_by_email);
assert_eq!(your_order.item_number, order_template.item_number);
assert_eq!(your_order.count, 1);
}
}
+1 -1
View File
@@ -5,7 +5,7 @@ mod sausage_factory {
String::from("Ginger")
}
fn make_sausage() {
pub fn make_sausage() {
get_secret_recipe();
println!("sausage!");
}
+3
View File
@@ -5,6 +5,9 @@ mod delicious_snacks {
// TODO: Add the following two `use` statements after fixing them.
// use self::fruits::PEAR as ???;
// use self::veggies::CUCUMBER as ???;
pub use self::fruits::PEAR as fruit;
pub use self::veggies::CUCUMBER as veggie;
mod fruits {
pub const PEAR: &str = "Pear";
+2
View File
@@ -4,6 +4,8 @@
// TODO: Bring `SystemTime` and `UNIX_EPOCH` from the `std::time` module into
// your scope. Bonus style points if you can do it with one line!
// use ???;
use std::time::SystemTime;
use std::time::UNIX_EPOCH;
fn main() {
match SystemTime::now().duration_since(UNIX_EPOCH) {
+3 -2
View File
@@ -9,10 +9,11 @@ use std::collections::HashMap;
fn fruit_basket() -> HashMap<String, u32> {
// TODO: Declare the hash map.
// let mut basket =
let mut basket = HashMap::new();
// Two bananas are already given for you :)
basket.insert(String::from("banana"), 2);
basket.insert(String::from("apple"), 2);
basket.insert(String::from("mango"), 1);
// TODO: Put more fruits in your basket.
basket
+3 -1
View File
@@ -28,10 +28,12 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
Fruit::Pineapple,
];
for fruit in fruit_kinds {
for _fruit in fruit_kinds {
// TODO: Insert new fruits if they are not already present in the
// basket. Note that you are not allowed to put any type of fruit that's
// already present!
basket.insert(Fruit::Banana, 4);
basket.insert(Fruit::Pineapple, 3);
}
}