forked from sunface/rust-by-practice
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
6 changes: 6 additions & 0 deletions
6
xcy-solutions/14. 包和模块/14.2/hello-package4/src/back_of_house.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
5 changes: 5 additions & 0 deletions
5
xcy-solutions/14. 包和模块/14.2/hello-package4/src/front_of_house/hosting.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
2 changes: 2 additions & 0 deletions
2
xcy-solutions/14. 包和模块/14.2/hello-package4/src/front_of_house/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod hosting; | ||
pub mod serving; |
8 changes: 8 additions & 0 deletions
8
xcy-solutions/14. 包和模块/14.2/hello-package4/src/front_of_house/serving.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// 填空 | ||
// in lib.rs | ||
|
||
mod front_of_house { | ||
// 实现此模块 | ||
} |