Add an example for struct visibility

This commit is contained in:
Manuel Thalmann 2025-01-17 08:43:44 +01:00
parent 95c1144ca2
commit 150e2faf44

View file

@ -17,6 +17,20 @@ mod front_of_house {
}
mod back_of_house {
pub struct Breakfast {
pub toast: String,
seasonal_fruit: String,
}
impl Breakfast {
pub fn summer(toast: &str) -> Breakfast {
Breakfast {
toast: String::from(toast),
seasonal_fruit: String::from("peaches"),
}
}
}
fn fix_incorrect_order() {
cook_order();
super::deliver_order();
@ -26,9 +40,13 @@ mod back_of_house {
}
pub fn eat_at_restaurant() {
// Absolute path
crate::front_of_house::hosting::add_to_waitlist();
// Order a breakfast in the summer with Rye toast
let mut meal = back_of_house::Breakfast::summer("Rye");
// Change our mind about what bread we'd like
meal.toast = String::from("Wheat");
println!("I'd like {} toast please", meal.toast);
// Relative path
front_of_house::hosting::add_to_waitlist();
// The next line won't compile if we uncomment it; we're not allowed
// to see or modify the seasonal fruit that comes with the meal
// meal.seasonal_fruit = String::from("blueberries");
}