hkBst
hkBst

Reputation: 3360

How can I fix the broken use statement in chapter 2 of ``Entirely Too Many Lists''?

If I try to test the final code (including hidden code which wraps the whole file in a main function) of chapter 2 of Learning Rust With Entirely Too Many Lists I get this error:

error[E0432]: unresolved import `super::List`    --> src/second.rs:110:9
    | 110 |     use super::List;
    |         ^^^^^^^^^^^ no `List` in `second`

Turns out if I remove the hidden code that wraps a main function around the displayed code I can run tests just fine. This does raise the question of what the purpose was of this hidden code that broke the use statement. Also no variation on that use statement (::List, crate::List, List, no use statement) fixes the error if I leave the main function there.

Question: Can the use statement be fixed while the main remains there? If not why is the main function there at all?

Upvotes: 0

Views: 98

Answers (1)

kmdreko
kmdreko

Reputation: 60022

Can the use statement be fixed while the main remains there?

No, super will refer to the encompassing module, not the function scope. There is no path that can name it as far as I'm aware. You could wrap everything within main in another module, so super can find it that way:

fn main() {
    mod inner {
        struct List;
        mod tests {
            use super::List;
        }
    }
}

If not why is the main function there at all?

mdBook just mimics the behavior of rustdoc which does wrap code samples with fn main() if not provided. This is not typically a problem since rustdoc examples wouldn't normally have the typical mod tests structure. Its unfortunate that this isn't handled though.

Upvotes: 1

Related Questions