diff options
| author | hybrid <hybrid@hybridlabs.cc> | 2026-10-14 21:33:16 +0300 |
|---|---|---|
| committer | hybrid <hybrid@hybridlabs.cc> | 2026-10-14 21:33:16 +0300 |
| commit | 43ddbb814e679bf2adde3a8602624c207641dca2 (patch) | |
| tree | b2cc74f6827381bc29608a3143f6131b19477535 /libs/restaurant/src/lib.rs | |
| parent | 84fe604d3492420821e7afc0772c6d2fb4c2e046 (diff) | |
| download | rust-book-43ddbb814e679bf2adde3a8602624c207641dca2.tar.gz rust-book-43ddbb814e679bf2adde3a8602624c207641dca2.tar.bz2 rust-book-43ddbb814e679bf2adde3a8602624c207641dca2.zip | |
add: libs/restaurant, src/{hashmaps_01, piglatin}
Diffstat (limited to 'libs/restaurant/src/lib.rs')
| -rw-r--r-- | libs/restaurant/src/lib.rs | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/libs/restaurant/src/lib.rs b/libs/restaurant/src/lib.rs new file mode 100644 index 0000000..ca35d43 --- /dev/null +++ b/libs/restaurant/src/lib.rs @@ -0,0 +1,57 @@ +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"), + } + } + // just to test reading capabilities and resolve + // "field never read" warning + pub fn components(&self) { + let v = vec![1, 2, 3, 4]; + + let does_not_exist = &v[100]; + let does_not_exist = v.get(100); + println!("toast: {}, seasonal fruit: {}", + self.toast, self.seasonal_fruit) + } + } +} + +pub fn add(left: u64, right: u64) -> u64 { + left + right +} + +pub fn eat_at_restaurant() { + // 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); + + meal.components(); + crate::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"); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + let result = add(2, 2); + assert_eq!(result, 4); + } +} |
