Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

crate-module/use-pub #276

giscus[bot] bot announced in Book Comments
Jul 21, 2022 · 11 comments · 7 replies
Discussion options

crate-module/use-pub

Learning Rust By Practice, narrowing the gap between beginner and skilled-dev with challenging examples, exercises and projects.

https://zh.practice.rs/crate-module/use-pub.html

You must be logged in to vote

Replies: 11 comments 7 replies

Comment options

第三题妙啊

You must be logged in to vote
2 replies
Comment options

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吗?

Comment options

好问题,我也是发现这个神奇的点。不过先回答你的问题:不是 hello_package::eat_at_restaurant() 可以,而是必须得用这个,因为如果用 hyphen -(减号)的话,编译器会报错,感觉那个应该是个特殊符号,所以只能用下划线。但是这个 project 的名字又确实可以叫 hello-package(在 cargo new 的时候),我推测有两个方法:

  1. 去 Cargo.toml 里修改 name 为 "hello_package"。
  2. 其实不需要修改任何东西,可以直接用 hello_package,编译器貌似会自动匹配上 hello-package。所以作者的这个写法其实是不会报错的,不管文件夹名是 hello-package 还是 hello_package
Comment options

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 吗?
You must be logged in to vote
1 reply
Comment options

pub mod hosting;

pub use hosting::eat_at_restaurant;

Comment options

lala

You must be logged in to vote
0 replies
Comment options

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、略

You must be logged in to vote
2 replies
Comment options

你这第二种太秀了

Comment options

答案里是直接用 *

Comment options

done

You must be logged in to vote
0 replies
Comment options

第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!");
}
You must be logged in to vote
2 replies
Comment options

不是,hosting是在其他文件中

Comment options

你这个结构感觉不太对,最符合原题的应该是没有 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 中

Comment options

done~~~

You must be logged in to vote
0 replies
Comment options

第3题要注意是要在 库crate lib.rs 中添加代码,而不是在 main.rs 中添加

You must be logged in to vote
0 replies
Comment options

mark finished

You must be logged in to vote
0 replies
Comment options

第三题是考察导出,因为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!")
}
You must be logged in to vote
0 replies
Comment options

done.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /