Hello guys! I'm currently trying to understand the usage of extern crate.
The rust-reference rust-reference says:
An extern crate declaration specifies a dependency on an external crate. (...)
So is it like adding a new dependency by adding it to Cargo.toml? What are its usages? If I understood it correctly, it just brings the given crate into our current scope, so we can write use theCrate::stuff; for example.
extern crate was necessary in the 2015 edition to actually use a dependency you declared in Cargo.toml. Since the 2018 edition crates will implicitly be loaded at the first reference. Using extern crate is thus not necessary anymore in (almost) all cases and is advised against.
Think of extern crate as a declaration inside your code that tells the compiler you want to link to another crate so you can use its functionality, kinda like the -l argument for clang or gcc. Whereas the [dependencies] section in Cargo.toml will orchestrate the downloading and building of your dependencies.
Most of the time what your crate needs to link with can be inferred from your Cargo.toml, so in the 2018 edition it was decided to make this extern crate implicit to reduce the boilerplate needed to use a dependency and match intuition ("I've already added foo to my Cargo.toml so why do I also need to write extern crate foo at the top of my lib.rs?").
However, some crates aren't present in your Cargo.toml because they are distributed with the Rust toolchain instead of being downloaded from crates.io. Some examples of these are the std, alloc, core, and proc_macro crates, although extern crate std will be injected automatically with the prelude. You can also do extern crate rustc to use the Rust compiler as a library, but that requires a #![feature(rustc_private)] because the compiler's internals are perma-unstable.
This isn't 100% true and I'm hand waving a bit, but the analogy should help build intuition anyway. ↩︎