Commit 7dd4507280a5754c19830c7b3a11cb9f2971c27a
1 parent
d068e5d3
Lots more restaurant stuff regarding modules
Showing
1 changed file
with
74 additions
and
3 deletions
... | ... | @@ -20,14 +20,85 @@ |
20 | 20 | // |
21 | 21 | |
22 | 22 | mod front_of_house { |
23 | - mod hosting { | |
24 | - fn add_to_waitlist() {} | |
23 | + pub mod hosting { | |
24 | + pub fn add_to_waitlist() {} | |
25 | 25 | fn seat_at_table() {} |
26 | 26 | } |
27 | 27 | |
28 | 28 | mod serving { |
29 | 29 | fn take_order() {} |
30 | - fn serve_order() {} | |
31 | 30 | fn take_payment() {} |
31 | + | |
32 | + } | |
33 | +} | |
34 | + | |
35 | +fn serve_order() {} | |
36 | + | |
37 | +mod back_of_house { | |
38 | + pub struct Breakfast { | |
39 | + pub toast: String, | |
40 | + seasonal_fruit: String, | |
41 | + } | |
42 | + | |
43 | + impl Breakfast { | |
44 | + pub fn summer(toast: &str) -> Breakfast { | |
45 | + Breakfast { | |
46 | + toast: String::from(toast), | |
47 | + seasonal_fruit: String::from("peaches"), | |
48 | + } | |
49 | + } | |
50 | + } | |
51 | + | |
52 | + pub enum Appetizer { | |
53 | + Soup, | |
54 | + Salad, | |
32 | 55 | } |
56 | + | |
57 | + fn fix_incorrect_order() { | |
58 | + cook_order(); | |
59 | + super::serve_order(); | |
60 | + } | |
61 | + | |
62 | + fn cook_order() {} | |
63 | +} | |
64 | + | |
65 | +// Either absolute: | |
66 | +// use crate::front_of_house::hosting; | |
67 | +// or relaive: | |
68 | +use front_of_house::hosting; | |
69 | +// we can also provide new names... | |
70 | +use crate::front_of_house::hosting as host; | |
71 | +// Public definitions braught into scope with use are | |
72 | +// private in this scope except we use «pub use» | |
73 | +pub use front_of_house::hosting as pubhost; | |
74 | + | |
75 | +pub fn eat_at_restaurant() { | |
76 | + // Absolute path | |
77 | + crate::front_of_house::hosting::add_to_waitlist(); | |
78 | + | |
79 | + // Relative path | |
80 | + front_of_house::hosting::add_to_waitlist(); | |
81 | + | |
82 | + // With use'd path | |
83 | + hosting::add_to_waitlist(); | |
84 | + | |
85 | + // With renamed used path | |
86 | + host::add_to_waitlist(); | |
87 | + | |
88 | + pubhost::add_to_waitlist(); | |
89 | + | |
90 | + // Order a breakfast in the summer with Rye toast | |
91 | + let mut meal = back_of_house::Breakfast::summer("Rye"); | |
92 | + | |
93 | + // Change our mind about what bread we'd like | |
94 | + meal.toast = String::from("Wheat"); | |
95 | + | |
96 | + println!("I'd like {} toast please", meal.toast); | |
97 | + | |
98 | + // The next line won't compile if we uncomment it; we're not allowed | |
99 | + // to see or modify the seasonal fruit that comes with the meal | |
100 | + // meal.seasonal_fruit = String::from("blueberries"); | |
101 | + | |
102 | + let order1 = back_of_house::Appetizer::Soup; | |
103 | + let order2 = back_of_house::Appetizer::Salad; | |
33 | 104 | } | ... | ... |
Please
register
or
login
to post a comment