Silly module question on serde

I saw mod lib define at root level here,
but I saw tons of pattern to use use lib::* (one reference) all over, I don't think child module natively inherit names from any parent modules. I don't see any super or crate across codes.

It is unclear what you are asking. Please rephrase your question in a way that it's comprehensible. If you are having trouble expressing yourself reasonably in English, please seek (human) help.

hmm, I think I want to ask based on my understanding child module do not automatically inherit names from its parent modules. How it is working in serde without using self super kind of things.

You're right in general that modules defined in a parent module are not exposed directly to child modules. However modules at the top level of the crate are accessible from the whole crate when compiling for the 2015 edition of Rust. Serde appears to be using the 2015 edition, which is why it works there.

I don't remember the history off the top of my head, but the module system has some fairly major changes for the 2018 edition.

1 Like

Here's an example:
Playground (2015 edition)
Playground (2018 edition)

mod lib {
    pub fn f() {}

mod a {
    mod b {
        use lib::f;

    mod c {
        use b::f;

On the 2015 edition use lib::f works, but use b::f doesn't. From the 2018 edition onward both uses produce an error.

"use lib::f" technically should have been "pub use lib::f" but it doesn't make a difference in the errors so oh well
1 Like

Thanks semicoleon, it really helps! I try few examples in my local on 2021 version, which doesn't tolerate that children modules reference names of root level parents modules with no relative syntax. Seem like 2015 had some loose rules.

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.