ch7 done
This commit is contained in:
parent
dacb5fa112
commit
41d6c74c1f
7
ch7/backyard/Cargo.lock
generated
Normal file
7
ch7/backyard/Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "backyard"
|
||||
version = "0.1.0"
|
6
ch7/backyard/Cargo.toml
Normal file
6
ch7/backyard/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "backyard"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
1
ch7/backyard/src/garden.rs
Normal file
1
ch7/backyard/src/garden.rs
Normal file
@ -0,0 +1 @@
|
||||
pub mod vegetables;
|
2
ch7/backyard/src/garden/vegetables.rs
Normal file
2
ch7/backyard/src/garden/vegetables.rs
Normal file
@ -0,0 +1,2 @@
|
||||
#[derive(Debug)]
|
||||
pub struct Asparagus {}
|
8
ch7/backyard/src/main.rs
Normal file
8
ch7/backyard/src/main.rs
Normal file
@ -0,0 +1,8 @@
|
||||
use crate::garden::vegetables::Asparagus;
|
||||
|
||||
pub mod garden;
|
||||
|
||||
fn main() {
|
||||
let plant = Asparagus {};
|
||||
println!("I'm growing: {plant:?}!");
|
||||
}
|
7
ch7/restaurant/Cargo.lock
generated
Normal file
7
ch7/restaurant/Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "restaurant"
|
||||
version = "0.1.0"
|
6
ch7/restaurant/Cargo.toml
Normal file
6
ch7/restaurant/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "restaurant"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
7
ch7/restaurant/src/front_of_house.rs
Normal file
7
ch7/restaurant/src/front_of_house.rs
Normal file
@ -0,0 +1,7 @@
|
||||
pub mod hosting;
|
||||
|
||||
mod serving {
|
||||
fn take_order() {}
|
||||
fn serve_order() {}
|
||||
fn take_payment() {}
|
||||
}
|
2
ch7/restaurant/src/front_of_house/hosting.rs
Normal file
2
ch7/restaurant/src/front_of_house/hosting.rs
Normal file
@ -0,0 +1,2 @@
|
||||
pub fn add_to_waitlist() {}
|
||||
fn seat_at_table() {}
|
92
ch7/restaurant/src/lib.rs
Normal file
92
ch7/restaurant/src/lib.rs
Normal file
@ -0,0 +1,92 @@
|
||||
// modules allow control of the privacy of items.
|
||||
// Code private by default. Can choose to make
|
||||
// modules and items within them public.
|
||||
//
|
||||
// Identifiers in crate paths are followed by ::
|
||||
|
||||
use std::collections::*;
|
||||
use std::fmt::Result;
|
||||
use std::io::Result as IoResult; // Can use this glob operator, not recommended tho. Typically only used in testing
|
||||
|
||||
pub fn add(left: u64, right: u64) -> u64 {
|
||||
left + right
|
||||
}
|
||||
|
||||
// #[cfg(test)]
|
||||
// mod tests {
|
||||
// use super::*;
|
||||
|
||||
// #[test]
|
||||
// fn it_works() {
|
||||
// let result = add(2, 2);
|
||||
// assert_eq!(result, 4);
|
||||
// }
|
||||
// }
|
||||
|
||||
mod front_of_house;
|
||||
|
||||
pub fn eat_at_restaurant() {
|
||||
crate::front_of_house::hosting::add_to_waitlist();
|
||||
front_of_house::hosting::add_to_waitlist();
|
||||
}
|
||||
|
||||
// starting relative paths with super
|
||||
fn deliver_order() {}
|
||||
|
||||
mod back_of_house {
|
||||
fn fix_incorrect_order() {
|
||||
cook_order();
|
||||
super::deliver_order();
|
||||
}
|
||||
|
||||
fn cook_order() {}
|
||||
|
||||
// if use pub before struct definition,
|
||||
// make struct public but fields private
|
||||
pub struct Breakfast {
|
||||
pub toast: String,
|
||||
seasonal_fruit: String,
|
||||
}
|
||||
|
||||
impl Breakfast {
|
||||
// because a single field is private, must provide a
|
||||
// public associated function that constructs an instance
|
||||
// of Breakfast
|
||||
pub fn summer(toast: &str) -> Breakfast {
|
||||
Breakfast {
|
||||
toast: String::from(toast),
|
||||
seasonal_fruit: String::from("peaches"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If make enum public, all of its variants are public.
|
||||
pub enum Appetizer {
|
||||
Soup,
|
||||
Salad,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn eat_at_restaurant_summer() {
|
||||
let mut meal = back_of_house::Breakfast::summer("Rye");
|
||||
meal.toast = String::from("Wheat");
|
||||
println!("I'd like {} toast plz", meal.toast);
|
||||
|
||||
// This line fails because not seasonal_fruit is private
|
||||
// meal.seasonal_fruit = String::from("blueberries");
|
||||
}
|
||||
|
||||
pub fn eat_at_restaurant_appetizer() {
|
||||
let order1 = back_of_house::Appetizer::Soup;
|
||||
let order2 = back_of_house::Appetizer::Salad;
|
||||
}
|
||||
|
||||
// The use statement only applies to the scope that it's in
|
||||
// use crate::front_of_house::hosting;
|
||||
|
||||
// Can also prepend the above statement with pub to allow for re-exporting
|
||||
pub use crate::front_of_house::hosting;
|
||||
|
||||
pub fn eat_at_restaurant_in_scope() {
|
||||
hosting::add_to_waitlist();
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user