In Rust, when `use shapes::*;` and `use plot::*;` both export `Point`, when does rustc report E0659, and what outranks a glob import?
answer
- errors at use, not import
- same item is not ambiguous
- items and named imports win
- prelude ranks lowest
basics
~20 sConflicting glob imports compile until the name is used; then rustc reports E0659, Point is ambiguous. A local item or an explicit use silently outranks any glob, and globs in turn outrank the standard prelude.
solid answer
~50 sGlob imports are allowed to import clashing names; the error comes only where the name is used: E0659, "`Point` is ambiguous", with a note pointing at both globs. If both globs reach the *same* item through re-exports, there is no ambiguity. An item declared in the module or a named import shadows any glob without complaint, so `use shapes::Point;` resolves the E0659, and a local `struct Point` silently wins over the glob's. The preludes sit below all of these: a glob that brings in a `Result` alias replaces the standard prelude's `Result` with no error. That ordering is why the standard prelude can grow per edition: the 2024 edition (Rust 1.85) added `Future` and `IntoFuture`, and any user name still wins. Traits are the exception to that safety, because new trait methods can make calls ambiguous.
code
rust · 22 linesmod shapes {
pub struct Point(pub i32);
pub struct Circle;
}
mod plot {
pub struct Point(pub i32);
pub struct Axis;
pub type Result<T> = std::result::Result<T, String>;
}
use shapes::*;
use plot::*; // both globs export `Point`: legal while unused
use shapes::Point; // an explicit import outranks both globs
fn check(n: i32) -> Result<i32> { // plot's alias outranks the prelude's Result
if n < 0 { Err(format!("negative: {n}")) } else { Ok(n) }
}
fn main() {
let p = Point(3); // shapes::Point, no E0659
let _parts = (Circle, Axis);
println!("{} {} {:?}", p.0, plot::Point(4).0, check(-1));
}go deeper
Know that use module::* imports every public name and that a clash between two globs only errors where the name is used.
Explain E0659, how an explicit import resolves it, and why a glob-imported Result alias replaces the prelude's.
Spot silent shadowing of glob names by local items in review, restrict globs to preludes and tests, and know why prelude traits wait for an edition.
Glob policy is a small team rule rather than a lead decision: allow globs for designed preludes and tests, and require explicit imports from external crates.
## What a glob import does `use shapes::*;` is a **glob import**: it binds every public name of `shapes` that the importing module can see. It is convenient for a library's prelude and for a test module's `use super::*;`, but it hides which names arrive, so rustc applies special rules to it. Explicit, named imports with a clashing name fail at once with E0252; globs do not. ## Conflicts surface at use, not at import - Two globs that export different items under one name compile. The name is simply unusable while ambiguous. - The first use fails with **E0659**: "`Point` is ambiguous", "ambiguous because of multiple glob imports of a name in the same module", with notes naming both globs and the help "consider adding an explicit import of `Point` to disambiguate". - If both globs lead to the **same item**, for example `shapes::Point` and a `prelude` module that re-exports it, nothing is ambiguous and the name resolves. - Names the globs do not share (`Circle` from one, `Axis` from the other) stay usable throughout. This lazy rule is what makes globs tolerable: adding an unrelated name to one module does not break every importer at once, only those who actually use that name through two globs. ## What outranks a glob Resolution within one module follows a ranking, highest first: | Source of the name | Shadows | Notes | |---|---|---| | Item declared in the module (`struct Point`) | globs, preludes | no warning about the shadowed glob name | | Named import (`use shapes::Point;`) | globs, preludes | clashes with an item are E0255 | | Glob import (`use plot::*;`) | preludes | glob versus glob is E0659 on use | | Standard, extern and language preludes | nothing | lowest priority | Two practical consequences: 1. **Resolving E0659 takes one line.** Adding `use shapes::Point;` beside the globs makes `Point` unambiguous, as rustc's help suggests. 2. **A local item hides a glob name silently.** With `use shapes::*;` and a local `struct Point(i32);`, `Point(5)` builds the local struct. rustc emits no error; at most the glob is reported as an unused import if nothing else from it is used. In review this is how a module ends up using a different `Point` from the one the author meant. ## The prelude at the bottom A **prelude** is a set of names queried automatically in every module without being members of it. The standard library prelude supplies `Option`, `Result`, `Vec`, `String`, `Box` and the common traits; the extern prelude supplies dependency crate names. Because preludes rank lowest, any item or import shadows them: - A crate's own `type Result<T> = std::result::Result<T, Error>;`, brought in by `use geometry::*;`, replaces the prelude's `Result` with no error. - The standard prelude can therefore grow without breaking type or value names. It is chosen **per edition**: `std::prelude::rust_2021` added `TryFrom`, `TryInto` and `FromIterator`, and `std::prelude::rust_2024`, stable with the 2024 edition in Rust 1.85, added `Future` and `IntoFuture`. On edition 2024, `fn ready() -> impl Future<Output = u8>` compiles with no import; on edition 2021 the same line fails with E0405 until `use std::future::Future;` is added. - **Traits are why additions wait for an edition.** A trait in scope contributes methods, and a new prelude trait can make an existing `x.try_into()` from a crate's own trait ambiguous, which is the reason the edition guide gives for putting `TryInto` in a new prelude. ## Reading an E0659 report 1. The two notes name the glob imports that compete; open both modules and decide which `Point` the code means. 2. Add a named import for that item, as the help line suggests; the explicit binding outranks both globs. 3. If neither glob is needed for much else, replace them with named imports altogether, which removes the next ambiguity before it appears. ## Guidance for glob imports - Use globs where the module was designed for them: a library's `prelude`, an enum's variants inside a `match`-heavy function, `use super::*;` in a test module. - Avoid globs of external crates' ordinary modules. Cargo's SemVer guide calls glob imports a known forward-compatibility hazard: a new trait in a dependency can break a glob importer's method calls without a major version bump. - When two globs must coexist, add explicit imports for the shared names up front rather than waiting for E0659. - Watch for local items whose names match a glob's; the silent shadow is legal and easy to miss.
- Why does a local `struct Point` not conflict with `use shapes::*;` when `shapes` also exports `Point`?Items and named imports are allowed to shadow glob imports in the same namespace, so the local struct wins and rustc reports no conflict. The risk is silence: code meant to use `shapes::Point` gets the local one, and the only hint may be an unused-import warning on the glob.
- How could the 2024 edition add `Future` to the standard prelude without breaking crates that define their own `Future`?Prelude names rank below every item and import, so a crate's own `Future` keeps shadowing the prelude's. What can break is method resolution, because traits in scope add methods; that risk is why new prelude traits arrive with an edition, `rust_2024` here, rather than in a regular release.
- Two globs both make `Point` visible, but one module re-exports the other's struct. Does using `Point` fail?No. Multiple glob imports may import the same name when they lead to the same item through re-exports, so `Point` resolves without E0659. Only distinct items under one name are ambiguous.
- Two globs exporting one name fail as soon as they are declared.
- A glob import overrides a struct declared in the same module.
- The standard prelude outranks imports, so you cannot define your own Result.
- Adding a name to the prelude breaks every crate that defines that name.
- Two globs reaching the same struct by re-export are always ambiguous.
Assumes Rust 1.99 on edition 2024, whose standard prelude added Future and IntoFuture (stable since 1.85); a 2021-edition crate must import them.