Does `extern crate` serve any purpose in the 2021 edition?

Are there any situations where extern crate needs no be used in the 2021 (or 2018) edition, or is it there purely for backwards compatibility?

See:

(click here to see full thread)

1 Like

extern crate is still useful if you want to import all macros from a crate like

#[macro_use] // to bring `anyhow!`, `bail!` and `ensure!` into scope
extern crate Anyhow;
1 Like

I would not recommend that. It's much less clear than use, and an artifact of old system.

1 Like

As in the other thread, extern crate alloc; is one use, as is #![no_std] #[cfg(feature = "std")] extern crate std;.

One use that I don't see often mentioned is extern crate self as name, which allows you to use ::name in addition to ::crate to refer to your own crate. This can be useful for e.g. macros, or even just for making internal code look more like external code.

7 Likes