Skip to content

Error messages about modules are confusing to beginners #50701

@jmikkola

Description

@jmikkola

When a beginner [0] first tries to use modules, they are likely to do it wrong. The error message printed is misleading because it only mentions extern crate foo and never mentions

Example

Consider the following project:

src/main.rs:

use foo::say_hello;

fn main() {
    say_hello();
}

src/foo.rs:

pub fn say_hello() {
    println!("Hello, world!");
}

Running cargo run results in the following error message:

   Compiling confusion v0.1.0 (file:///home/jeremy/tmp/confusion)
error[E0432]: unresolved import `foo`
 --> src/main.rs:1:5
  |
1 | use foo::say_hello;
  |     ^^^ Maybe a missing `extern crate foo;`?

error: aborting due to previous error

For more information about this error, try `rustc --explain E0432`.
error: Could not compile `confusion`.

To learn more, run the command again with --verbose.

Naturally, one might try adding extern crate foo; to the top of main.rs. This changes the error message to:

   Compiling confusion v0.1.0 (file:///home/jeremy/tmp/confusion)
error[E0463]: can't find crate for `foo`
 --> src/main.rs:1:1
  |
1 | extern crate foo;
  | ^^^^^^^^^^^^^^^^^ can't find crate

error: aborting due to previous error

For more information about this error, try `rustc --explain E0463`.
error: Could not compile `confusion`.

To learn more, run the command again with --verbose.

If a user reads the output of rustc --explain E0432, they might instead try changing main.rs to start with use self::foo::say_hello;, though this does not change the error message.

Another common thing to try is adding mod foo; or pub mod foo; to the top of foo.rs. It is very unlikely that a user will stumble upon an actual solution (such as adding mod foo; to the top of main.rs).

Suggestion

The error message should also mention how to refer to local modules, not just external crates.

If there is a file that matches the module name the user is trying to import, it would be especially helpful if the error message discussed how to correctly use that module from main.rs.

Meta

$ rustc --version
rustc 1.26.0 (a77568041 2018-05-07)

[0] or myself, after having not touched Rust in over a year

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions