restaurant: Add example on modules/structure/...
This commit is contained in:
parent
9a3afcec47
commit
5a7dfb8ba1
7 changed files with 83 additions and 0 deletions
9
restaurant/Cargo.toml
Normal file
9
restaurant/Cargo.toml
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
[package]
|
||||
name = "restaurant"
|
||||
version = "0.1.0"
|
||||
authors = ["laurens <miers132@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
1
restaurant/src/back_of_house.rs
Normal file
1
restaurant/src/back_of_house.rs
Normal file
|
|
@ -0,0 +1 @@
|
|||
pub mod cooking;
|
||||
28
restaurant/src/back_of_house/cooking.rs
Normal file
28
restaurant/src/back_of_house/cooking.rs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
pub struct Breakfast { // Struct is public but its members aren't!
|
||||
pub toast: String, // Customer can choose which toast he wants
|
||||
seasonal_fruit: String, // Only chef can choose fruit that is served
|
||||
}
|
||||
|
||||
pub enum Appetizer { // Enum is public and its possible variants too!
|
||||
Soup,
|
||||
Salad,
|
||||
}
|
||||
|
||||
fn do_nothing() {}
|
||||
|
||||
fn fix_incorrect_order() {
|
||||
cook_order();
|
||||
|
||||
crate::serve_order();
|
||||
}
|
||||
|
||||
fn cook_order() {}
|
||||
|
||||
impl Breakfast {
|
||||
pub fn summer(toast: &str) -> Breakfast {
|
||||
Breakfast {
|
||||
toast: String::from(toast),
|
||||
seasonal_fruit: String::from("peaches"),
|
||||
}
|
||||
}
|
||||
}
|
||||
1
restaurant/src/front_of_house.rs
Normal file
1
restaurant/src/front_of_house.rs
Normal file
|
|
@ -0,0 +1 @@
|
|||
pub mod hosting;
|
||||
1
restaurant/src/front_of_house/hosting.rs
Normal file
1
restaurant/src/front_of_house/hosting.rs
Normal file
|
|
@ -0,0 +1 @@
|
|||
pub fn add_to_waitlist() {}
|
||||
42
restaurant/src/lib.rs
Normal file
42
restaurant/src/lib.rs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
use std::collections::*;
|
||||
use std::{self, cmp::Ordering, io};
|
||||
|
||||
mod front_of_house;
|
||||
|
||||
pub use crate::front_of_house::hosting;
|
||||
// pub use front_of_house::hosting;
|
||||
|
||||
mod back_of_house;
|
||||
|
||||
use crate::back_of_house::cooking::Breakfast as boh_breakfast;
|
||||
use back_of_house::cooking;
|
||||
|
||||
mod util;
|
||||
|
||||
pub fn eat_at_restaurant() {
|
||||
// // Absolute path
|
||||
crate::front_of_house::hosting::add_to_waitlist();
|
||||
|
||||
// // Relative path
|
||||
front_of_house::hosting::add_to_waitlist();
|
||||
|
||||
// // Making use of the 'use' keyword
|
||||
hosting::add_to_waitlist();
|
||||
|
||||
// Order a breakfast in the summer with Rye toast
|
||||
let mut meal = boh_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);
|
||||
|
||||
// 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");
|
||||
|
||||
let order1 = cooking::Appetizer::Soup;
|
||||
let order2 = cooking::Appetizer::Salad;
|
||||
|
||||
util::get_cutlery();
|
||||
}
|
||||
|
||||
fn serve_order() {}
|
||||
1
restaurant/src/util.rs
Normal file
1
restaurant/src/util.rs
Normal file
|
|
@ -0,0 +1 @@
|
|||
pub fn get_cutlery() {}
|
||||
Loading…
Add table
Add a link
Reference in a new issue