crate-module/use-pub #276
Replies: 11 comments 7 replies
-
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.
-
1、 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.
-
第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.
-
第3题要注意是要在 库crate lib.rs 中添加代码,而不是在 main.rs 中添加 |
Beta Was this translation helpful? Give feedback.
-
mark finished |
Beta Was this translation helpful? Give feedback.
-
第三题是考察导出,因为
所以再导出即可: 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.
-
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
Beta Was this translation helpful? Give feedback.
All reactions