When to use extern create crateName, mod crateName or just use crateName::SomeFromCrate

In my Cargo.toml I have:

[dependencies]
log = "*"

then in my .rs file I have:

use log::{debug, error, info, log_enabled, Level};

And all is well and dandy and it works.
Then, I add into Cargo.toml

term = "*"

and this time I cannot (or can I?) do just:

use term::Something

Instead I have to do:

extern crate term;

Why?

Do you or don't you get a compilation error without extern crate term? I have no problem compiling without it. Make sure you have edition = "2018" in Cargo.toml, which -- among other things -- allows you to omit extern crate in almost all circumstances (the exceptions are crates that come with the standard library: std, alloc).

Edition differences aside, these two snippets always do the same thing, unless you have another item named foo in scope:

use crate_name::foo;
// ...
fn main() {
    foo();
}
// ...
fn main() {
    crate_name::foo();
}

mod name does something completely different: it declares a module in the current crate.

3 Likes