-
Notifications
You must be signed in to change notification settings - Fork 1.1k
crate-module/use-pub #276
-
crate-module/use-pub
Learning Rust By Practice, narrowing the gap between beginner and skilled-dev with challenging examples, exercises and projects.
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1 -
🎉 1
Replies: 11 comments 7 replies
-
第三题妙啊
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
mod front_of_house;
fn main() {
assert_eq!(front_of_house::hosting::seat_at_table(), "sit down please");
assert_eq!(hello_package::eat_at_restaurant(),"yummy yummy!");
}
hello_package::eat_at_restaurant()为什么可以?这个包不是叫hello-package吗?
Beta Was this translation helpful? Give feedback.
All reactions
-
好问题,我也是发现这个神奇的点。不过先回答你的问题:不是 hello_package::eat_at_restaurant()
可以,而是必须得用这个,因为如果用 hyphen -
(减号)的话,编译器会报错,感觉那个应该是个特殊符号,所以只能用下划线。但是这个 project 的名字又确实可以叫 hello-package(在 cargo new 的时候),我推测有两个方法:
- 去 Cargo.toml 里修改 name 为 "hello_package"。
- 其实不需要修改任何东西,可以直接用
hello_package
,编译器貌似会自动匹配上hello-package
。所以作者的这个写法其实是不会报错的,不管文件夹名是hello-package
还是hello_package
Beta Was this translation helpful? Give feedback.
All reactions
-
fn main() { assert_eq!(front_of_house::hosting::seat_at_table(), "sit down please"); assert_eq!(hello_package::eat_at_restaurant(),"yummy yummy!"); }``` hello_package::eat_at_restaurant() 为什么可以?这个包不是叫 hello-package 吗?
Beta Was this translation helpful? Give feedback.
All reactions
-
pub mod hosting;
pub use hosting::eat_at_restaurant;
Beta Was this translation helpful? Give feedback.
All reactions
-
lala
Beta Was this translation helpful? Give feedback.
All reactions
-
1、as
可以重命名
use std::fmt::Result; use std::io::Result as ioResult; fn main() {}
2、第一种{}
use std::collections::{HashMap,BTreeMap,HashSet}; fn main() { let _c1:HashMap<&str, i32> = HashMap::new(); let mut c2 = BTreeMap::new(); c2.insert(1, "a"); let _c3: HashSet<i32> = HashSet::new(); }
第二种,展开
use std::collections::HashMap;use std::collections::BTreeMap;use std::collections::HashSet; fn main() { let _c1:HashMap<&str, i32> = HashMap::new(); let mut c2 = BTreeMap::new(); c2.insert(1, "a"); let _c3: HashSet<i32> = HashSet::new(); }
3、略
Beta Was this translation helpful? Give feedback.
All reactions
-
你这第二种太秀了
Beta Was this translation helpful? Give feedback.
All reactions
-
答案里是直接用 *
Beta Was this translation helpful? Give feedback.
All reactions
-
done
Beta Was this translation helpful? Give feedback.
All reactions
-
第3题不是这样的吗?
mod hello_package { pub mod hosting { pub fn seat_at_table() -> &'static str { "sit down please" } } pub fn eat_at_restaurant() -> &'static str { "yummy yummy!" } } fn main() { assert_eq!(hello_package::hosting::seat_at_table(), "sit down please"); assert_eq!(hello_package::eat_at_restaurant(),"yummy yummy!"); }
Beta Was this translation helpful? Give feedback.
All reactions
-
不是,hosting是在其他文件中
Beta Was this translation helpful? Give feedback.
All reactions
-
你这个结构感觉不太对,最符合原题的应该是没有 mod hello_package
,这个 project 的名字就是 hello_package
(Cargo.toml 里的 name
)。
所以 eat_at_restaurant 是和 main 同级的,然后hosting 上层还有一个 mod front_of_house {
(front_of_house 和 main 同级)。这时如果你想通过 hello_package::hosting::
而不是 hello_package::front_of_house::hosting
访问到 seat_at_table
的话,就需要用一个 use hello_package/crate/self::front_of_house::hosting
,你这里不需要加 pub use
是因为你的 main 函数和模块定义是在同一个地方,原题中的 main 函数在 main.rs 中,其余的在 lib.rs 中
Beta Was this translation helpful? Give feedback.
All reactions
-
done~~~
Beta Was this translation helpful? Give feedback.
All reactions
-
第3题要注意是要在 库crate lib.rs 中添加代码,而不是在 main.rs 中添加
Beta Was this translation helpful? Give feedback.
All reactions
-
mark finished
Beta Was this translation helpful? Give feedback.
All reactions
-
第三题是考察导出,因为lib.rs
可以直接被main.rs
调用的,而lib.rs
已经导入了front_of_house
。而main.rs
无法使用此模块内容是因为:
当外部的模块项 A 被引入到当前模块中时,它的可见性自动被设置为私有的
所以再导出即可:
mod backof_house; mod front_of_house; // in lib.rs // Add this line // 导出此模块,使其可见 pub use crate::front_of_house::hosting; pub fn eat_at_restaurant()->String{ front_of_house::hosting::add_to_waitlist(); backof_house::cook_order(); String::from("yummy yummy!") }
Beta Was this translation helpful? Give feedback.
All reactions
-
done.
Beta Was this translation helpful? Give feedback.