skip to content

A Rust binary declares `mod geometry` and depends on a crate also named `geometry`; what does `use geometry::origin;` resolve to in each module?

level: seniorimportance: nice to knowfreq 25%

answer

  1. the extern prelude
  2. module items shadow preludes
  3. leading :: means crates only
  4. same text, different module

basics

~20 s

At the root, the local mod geometry shadows the crate, so the import fails with E0432 if the module lacks origin; in nested modules it reaches the dependency. Write ::geometry for the crate and crate::geometry for the module.

solid answer

~40 s

Dependency crates live in the **extern prelude**: Cargo passes each one to rustc with `--extern`, so its name is usable from every module without `extern crate`. Like every prelude, it ranks below the names a module declares. At the crate root, `mod geometry` therefore shadows the crate, and `use geometry::origin;` looks in the local module (E0432 if `origin` is not there). Inside `mod render`, root items are not in scope, so the same line reaches the dependency. One path text, two meanings, depending on the module. Make intent explicit: `::geometry::origin` (a leading `::` reaches only the extern prelude since 2018) for the crate, and `crate::geometry::local` for the module. In edition 2024, `extern crate` remains for sysroot crates Cargo does not pass, such as `alloc`, and for `extern crate x as y` aliases.

code

rust · 21 lines
rust
// Built with: rustc --edition 2024 main.rs --extern geometry=libgeometry.rlib
// The `geometry` crate exports `pub fn origin() -> Point`.
mod geometry {
    pub fn local() -> &'static str { "local module" }
}

mod render {
    // Crate-root items are not in scope here, so `geometry`
    // falls through to the extern prelude: the dependency.
    use geometry::origin;
    pub fn draw() -> f64 { origin().x }
}

use ::geometry::origin as crate_origin;   // `::` = extern prelude only
use crate::geometry::local;               // `crate::` = this crate's module

fn main() {
    // At the root the local module shadows the crate:
    // `geometry::origin()` here would be E0425.
    println!("{} {} {}", render::draw(), crate_origin().y, local());
}

go deeper

for a junior

Know that dependency crates are usable by name in any module without extern crate, and that crate:: always means your own crate.

for a middle

Explain the extern prelude, that a leading :: means a crate since 2018, and that module items shadow prelude names.

for a senior

Diagnose an import that compiles in one module and fails in another because a local module shadows a crate, and standardise ::name and crate::name spellings.

for a principal

There is no lead decision here beyond naming hygiene: avoid giving internal modules the names of dependencies so paths never depend on location.

## Where crate names come from Every dependency a Cargo package declares is handed to rustc with an `--extern name=path` flag. rustc puts each of those names in the **extern prelude**, one of the preludes that every module consults during name resolution. Since the 2018 edition that is all a crate needs: `use serde::Serialize;` or `geometry::origin()` works in any module, with no `extern crate` line. Two crates are always present: `core`, and `std` unless the crate root has `#![no_std]`. A prelude's names are **not members** of any module. They are consulted after the module's own items and imports, which has two effects worth knowing: - An item declared in a module **shadows** a prelude name. A module named `geometry` hides a crate named `geometry` in that module. - `self::std::mem::size_of::<u8>()` fails with E0433, "cannot find `std` in `self`": `std` comes from the extern prelude, not from the current module. ## Same path, two answers Take a binary whose root declares `mod geometry { pub fn local() ... }` and whose dependency `geometry` exports `origin()`: | Where the code is | `geometry::...` resolves to | `use geometry::origin;` | |---|---|---| | The crate root, which declares `mod geometry` | the local module | E0432, no `origin` in `geometry` | | A nested `mod render` | the dependency crate | compiles | | Any module that itself imports or declares `geometry` | that binding | depends on it | The nested case works because items of an outer module are not in scope in an inner one; with no local `geometry` in sight, resolution falls through to the extern prelude. Each answer is deterministic, so rustc reports no ambiguity in these cases, and the same line of code changes meaning when moved between modules. That is the review hazard. ## Writing the intent explicitly - `::geometry::origin` — a leading `::` resolves **only** from the extern prelude since the 2018 edition, so it always means the crate. In the 2015 edition it meant the crate root, which is why 2015-era code reads differently. - `crate::geometry::local` — always the module declared at the crate root. - `self::geometry` and `super::geometry` — a module relative to the current or parent module, never a crate. - Renaming one side removes the clash for good: rename the local module, or import the crate under another name (`use ::geometry as geo;`). ## When `extern crate` is still written Most `extern crate` lines in edition 2024 code are leftovers, but three uses remain: 1. **Sysroot crates Cargo does not pass.** `alloc` (and `test`) ship with rustc, but Cargo does not pass them with `--extern`, so they are not in the extern prelude, so a `no_std` library that wants `Vec` writes `extern crate alloc;` and then `use alloc::vec::Vec;`. Cargo does add `proc_macro` to the extern prelude, but only for proc-macro crates. 2. **Aliases.** `extern crate geometry as geo;` in the crate root puts `geo` in the extern prelude, usable from every module. Renaming a dependency in `Cargo.toml` achieves a similar effect and belongs to Cargo. 3. **Linking without naming.** `extern crate some_crate as _;` or `use some_crate as _;` links a crate whose side effects you need without binding its name. ## Macros by path The 2015 edition imported a crate's exported macros with `#[macro_use] extern crate some_crate;`, which fills the separate **macro_use prelude** with every exported macro of that crate. Since the 2018 edition a `macro_rules!` macro exported from a dependency is imported by path like any item, `use some_crate::some_macro;`, or called with a full path, `some_crate::some_macro!(...)`. The same shadowing rules then apply to the macro's name as to any other imported name, which makes the old attribute another `extern crate` leftover to remove when modernising a crate. ## Practical checklist - When a module and a dependency share a name, choose one spelling for each throughout the crate (`::name` and `crate::name`) instead of relying on where code happens to sit. - When moving code between modules, re-check imports whose first segment matches a local module name. - Treat a plain `extern crate` for an ordinary crates.io dependency as dead code in edition 2018 and later; delete it. - A crate's package name with hyphens is referred to with underscores in paths, a Cargo naming convention; the resolution rules above apply to the resulting identifier.

  • When does a Rust 2024 crate still need `extern crate`?
    For sysroot crates that Cargo does not pass with `--extern`, chiefly `alloc` in a `no_std` library (and `test`); for an alias such as `extern crate geometry as geo;` that every module can use; and for linking a crate only for its side effects with `as _`. Ordinary dependencies need no `extern crate`.
  • What did a leading `::` mean in Rust 2015, and what does it mean now?
    In 2015, `::a::f()` started at the crate root, so it reached the crate's own module `a`. Since 2018 a leading `::` resolves only from the extern prelude, so `::a` must be a crate. Code moved from 2015 can therefore change meaning or stop compiling, which edition migration flags.
  • Why does `self::std::mem::size_of::<u8>()` fail when `std::mem::size_of::<u8>()` works?
    `std` is in the extern prelude, and prelude names are consulted during resolution without being members of any module. `self::` asks the current module for a member called `std`, finds none and reports E0433, "cannot find `std` in `self`".

  • A dependency needs extern crate before use can name it.
  • In Rust 2024, a leading :: means the current crate's root.
  • A shared module and crate name always gives an ambiguity error.
  • crate::geometry names the dependency called geometry.
  • Using both requires renaming the local module.