Why can not the shortend path be used here?

The code in lib.rs is like this:

pub mod front_of_house {
    pub fn deliver() {}

    mod customer {
      use crate::front_of_house;//why do I need use full path here
      // use front_of_house;//shorten path get compile error

      pub fn eat_at_restaurant () {
        front_of_house::deliver();
      }
    }
}

The error message is:

error[E0432]: unresolved import `front_of_house`
  --> src/lib.rs:67:11
   |
67 |       use front_of_house;
   |           ^^^^^^^^^^^^^^ no external crate `front_of_house`

It looks like the child mod don't recognize the parental mod name, which is in the same file. Usually, when you define and use a mod in a file, like main.rs, you don't need to specify the full path, right?
Is it designed like this? Or did I do something wrong? Why is it like this?
Thanks.

Use statements start from the top-level, looking for crates, i.e., the first component is assumed to be a crate by default, except for some special keywords. So, if you want to access:

  • the current crate, use crate::module;
  • the current module, use self::submodule;
  • the parent module, use super::sibling_module;
5 Likes

I see, thanks!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.