HiveBrain v1.2.0
Get Started
← Back to all entries
gotcharustCritical

What's the difference between use and extern crate?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
andexternbetweenusethedifferencecratewhat

Problem

I think that use is used to import identifiers into the current scope and extern crate is used to declare an external module. But this understanding (maybe wrong) doesn't make any sense to me. Can someone explain why Rust has these two concepts and what are the suitable cases to use them?

Solution

extern crate foo indicates that you want to link against an external library and brings the top-level crate name into scope (equivalent to use foo). As of Rust 2018, in most cases you won't need to use extern crate anymore because Cargo informs the compiler about what crates are present. (There are one or two exceptions)

use bar is a shorthand for referencing fully-qualified symbols.

Theoretically, the language doesn't need use — you could always just fully-qualify the names, but typing std::collections::HashMap.new(...) would get very tedious! Instead, you can just type use std::collections::HashMap once and then HashMap will refer to that.

Context

Stack Overflow Q#29403920, score: 217

Revisions (0)

No revisions yet.