Using "extern crate foo" locally

Hi @all,

$ rustc --version
rustc 1.21.0 (3b72af97e 2017-10-09)

fn f() {
extern crate foo;

this compiles

fn f() {
extern crate foo;
use foo::{bar};

this does not compile and gives an error
68 | use foo::{bar};
| ^^^^^^^^ Maybe a missing extern crate foo;?

after adding "extern crate foo;" at top level before f this error can be resovled.

Is this behavour by design?

I would expect that either "extern crate foo;" should not compile when used inside f OR
it should compile and allow "use foo::{bar};"

Has this been already discussed?


In general, this use confusion is common, and may be addressed in a future Rust "epoch". A use path is based from the crate root, unless it starts with self::... (for the current module) or super::... (for the parent module).

So if your extern crate foo; was in the module scope, you could use self::foo::bar; regardless of whether this code is the crate root. But at the fn scope, I'm actually not sure if there is a way to use it!

1 Like