Skip to content

Commit

Permalink
add 14.2
Browse files Browse the repository at this point in the history
  • Loading branch information
xuexcy committed Aug 31, 2024
1 parent 4b054a7 commit c6d31ff
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 0 deletions.
6 changes: 6 additions & 0 deletions xcy-solutions/14. 包和模块/14.2/hello-package/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "hello-package"
version = "0.1.0"
edition = "2021"

[dependencies]
50 changes: 50 additions & 0 deletions xcy-solutions/14. 包和模块/14.2/hello-package/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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 mod hosting {
pub fn add_to_waitlist() {}
fn seat_at_table() {}
}
pub mod serving {
fn take_order() {}
pub fn serve_order() {}
fn take_payment() {}
fn complain() {}
}
}

fn eat_at_restaurant() {
// 使用绝对路径调用
crate::front_of_house::hosting::add_to_waitlist();

// 使用相对路径调用
front_of_house::hosting::add_to_waitlist();
}

// in lib.rs

mod back_of_house {
fn fix_incorrect_order() {
cook_order();
// 使用2种方式填空
//1. 使用关键字 `super`
//2. 使用绝对路径
crate::front_of_house::serving::serve_order();
super::front_of_house::serving::serve_order();
}

fn cook_order() {}
}
6 changes: 6 additions & 0 deletions xcy-solutions/14. 包和模块/14.2/hello-package4/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "hello-package4"
version = "0.1.0"
edition = "2021"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub fn fix_incorrect_order() {
cook_order();
crate::front_of_house::serving::serve_order();
}

pub fn cook_order() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub fn add_to_waitlist() {}

pub fn seat_at_table() -> String {
String::from("sit down please")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod hosting;
pub mod serving;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pub fn take_order() {}

pub fn serve_order() {}

pub fn take_payment() {}

// 我猜你不希望顾客听到你在抱怨他们,因此让这个函数私有化吧
fn complain() {}
12 changes: 12 additions & 0 deletions xcy-solutions/14. 包和模块/14.2/hello-package4/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// in lib.rs
mod front_of_house;
mod back_of_house;


pub fn eat_at_restaurant() -> String {
front_of_house::hosting::add_to_waitlist();

back_of_house::cook_order();

String::from("yummy yummy!")
}
17 changes: 17 additions & 0 deletions xcy-solutions/14. 包和模块/14.2/hello-package4/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// // in src/main.rs
//
// // 填空并修复错误
// fn main() {
// assert_eq!(__, "sit down please");
// assert_eq!(__,"yummy yummy!");
// }

// in src/main.rs

mod front_of_house;

// 填空并修复错误
fn main() {
assert_eq!(front_of_house::hosting::seat_at_table(), "sit down please");
assert_eq!(hello_package4::eat_at_restaurant(),"yummy yummy!");
}
6 changes: 6 additions & 0 deletions xcy-solutions/14. 包和模块/14.2/main1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// 填空
// in lib.rs

mod front_of_house {
// 实现此模块
}

0 comments on commit c6d31ff

Please sign in to comment.