Use and pub

  1. 🌟 We can bring two types of the same name into the same scope with use, but you need as keyword.
1. use std::fmt::Result;
2. use std::io::Result;

4. fn main() {}
  1. 🌟🌟 If we are using multiple items defined in the same crate or module, then listing each item on its own line will take up too much vertical space.
2. // FILL in the blank in two ways
3. // DON'T add new code line
4. use std::collections::__;

6. fn main() {
7. let _c1:HashMap<&str, i32> = HashMap::new();
8. let mut c2 = BTreeMap::new();
9. c2.insert(1, "a");
10. let _c3: HashSet<i32> = HashSet::new();
11. }

Re-exporting names with pub use

  1. 🌟🌟🌟 In our recently created package hello-package, add something to make the below code work
1. fn main() {
2. assert_eq!(hello_package::hosting::seat_at_table(), "sit down please");
3. assert_eq!(hello_package::eat_at_restaurant(),"yummy yummy!");
4. }

Pub(in Crate)

Sometimes we want an item only be public to a certain crate. For this we can use the pub(in Crate) syntax.

Example

1. pub mod a {
2. pub const I: i32 = 3;

4. fn semisecret(x: i32) -> i32 {
5. use self::b::c::J;
6. x + J
7. }

9. pub fn bar(z: i32) -> i32 {
10. semisecret(I) * z
11. }
12. pub fn foo(y: i32) -> i32 {
13. semisecret(I) + y
14. }

16. mod b {
17. pub(in crate::a) mod c {
18. pub(in crate::a) const J: i32 = 4;
19. }
20. }
21. }

Full Code

The full code of hello-package is here.

You can find the solutions here (under the solutions path), but only use it when you need it :)