crate-module/module #374
Replies: 12 comments 4 replies
-
虽然不能编辑,不过往好处想,反正编辑了也运行不了对不对,这下正好可以去本地实操运行了是不是 |
Beta Was this translation helpful? Give feedback.
-
mod front_of_house {
mod hosting {
fn add_to_waitlist() -> () {}
fn seat_at_table() -> () {}
}
mod serving {
fn take_order() -> () {}
fn serve_order() -> () {}
fn take_payment() -> () {}
fn complain() -> () {}
}
} 请教一下各位大佬,题目一 编译 报错 |
Beta Was this translation helpful? Give feedback.
-
第四题,为什么 back_of_house.rs引用front_of_house是use crate::font_of_house;,而不是pub mod font_of_house; ?
|
Beta Was this translation helpful? Give feedback.
-
最后一题: fn main() {
assert_eq!(restaurant::front_of_house::hosting::seat_at_table(), "sit down please");
assert_eq!(restaurant::eat_at_restaurant(), "yummy yummy!");
} 注意,此时lib.rs中,是使用pub关键字来mod front_of_house,否则第一行的assert_eq!会报错。除非我们在main.rs中mod front_of_house。 在src中创建restaurant文件夹我们的 // in src/restaurant/mod.rs
pub mod hello; // in src/restaurant/hello.rs
pub fn hello () {
println!("hello world");
} // in src/lib.rs
pub mod restaurant; // in src/main.rs
fn main() {
restaurant::restaurant::hello::hello(); // 第一个restaurant实际上是指lib.rs,第二个restaurant指的是文件夹restaurant,第一个hello指的是mod hello,第二个hello是函数hello()。
} |
Beta Was this translation helpful? Give feedback.
-
最后一题,这样可以吗(package hello-package): // in src/main.rs
use hello_package::{self, front_of_house::serving};
// 填空并修复错误
fn main() {
assert_eq!(serving::seat_at_table(), "sit down please");
assert_eq!(hello_package::eat_at_restaurant(),"yummy yummy!"); |
Beta Was this translation helpful? Give feedback.
-
mark finished |
Beta Was this translation helpful? Give feedback.
-
第三题应该是: mod back_of_house {
fn fix_incorrect_order() {
cook_order();
self::front_of_house::serving::serve_order();
}
fn cook_order() {}
} |
Beta Was this translation helpful? Give feedback.
-
crate-module/module
Learning Rust By Practice, narrowing the gap between beginner and skilled-dev with challenging examples, exercises and projects.
https://zh.practice.rs/crate-module/module.html
Beta Was this translation helpful? Give feedback.
All reactions