In Rust, why do `use crate::shapes::Point;` and `use crate::svg::Point;` in one module fail with E0252, and how do you fix it?
answer
- one name per namespace
- types, values, macros
- rename with as
- import the parent module
basics
~20 sEach module holds one binding per name in each namespace, and both imports bind Point in the type namespace, so rustc reports E0252 at the second use. Rename one with as, or import the parent module and write svg::Point.
solid answer
~40 sA module has separate **namespaces** for types, values and macros, and each may hold one binding per name. Both imports bind `Point` in the type namespace, so rustc reports E0252, "the name `Point` is defined multiple times", at the second `use`, whether or not `Point` is ever used. The fixes: rename one import (`use crate::svg::Point as SvgPoint;`), or import the parent module (`use crate::svg;`) and write `svg::Point`, which the Book recommends for same-named items such as `fmt::Result` and `io::Result`. Braces combine both: `use crate::svg::{self, Point as SvgPoint};`. A clash between an import and a local item is E0255. If only a trait's methods are needed, `use path::Trait as _;` brings it into scope without binding its name.
code
rust · 26 linesmod shapes {
pub struct Point { pub x: f64, pub y: f64 }
pub trait Area { fn area(&self) -> f64; }
pub struct Square(pub f64);
impl Area for Square { fn area(&self) -> f64 { self.0 * self.0 } }
}
mod svg {
pub struct Point { pub x: i32, pub y: i32 }
}
mod render {
use std::fmt::{self, Display};
use crate::shapes::{Point, Area as _};
use crate::svg::Point as SvgPoint;
use crate::svg;
pub struct Area; // a local item named Area: fine because the trait was imported as _
impl Display for Area { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "area") } }
pub fn to_svg(p: &Point) -> SvgPoint { SvgPoint { x: p.x as i32, y: p.y as i32 } }
pub fn also(p: &Point) -> svg::Point { svg::Point { x: p.x as i32, y: p.y as i32 } }
pub fn sq() -> f64 { crate::shapes::Square(3.0).area() }
}
fn main() {
let p = shapes::Point { x: 1.5, y: 2.5 };
let s = render::to_svg(&p);
let t = render::also(&p);
println!("{} {} {} {} {} {}", s.x, s.y, t.x, t.y, render::sq(), render::Area);
}go deeper
Recognise E0252 as two imports binding one name, and fix it with as or by importing the parent module.
Explain Rust's type, value and macro namespaces, why a tuple struct clashes with a function but a braced struct does not, and how E0255 differs.
Choose between aliasing and module-qualified names by how often each type appears, and use Trait as _ when only methods are needed.
Naming conflicts hold no lead decision on their own; a team rule might simply forbid vague aliases so each Point stays identifiable in review.
## The error Inside one module, two `use` declarations that bind the same name in the same namespace are rejected. rustc 1.99 reports it at the second import: ```plaintext error[E0252]: the name `Point` is defined multiple times = note: `Point` must be defined only once in the type namespace of this module help: you can use `as` to change the binding name of the import ``` The check happens when the imports are declared, not when the name is used: a module that imports both `Point`s and never mentions either still fails. That separates **explicit imports** from **glob imports** (`use a::*;`), whose conflicts are tolerated until a name is used; globs are a separate subject. ## Namespaces decide what counts as a clash Rust keeps several namespaces per module, so one name can mean different things in different positions: | Namespace | Holds | Example | |---|---|---| | Type | modules, structs, enums, traits, type aliases | `Point` in `let p: Point` | | Value | functions, constants, statics, tuple and unit struct constructors | `Point` in `Point(1.0, 2.0)` | | Macro | `macro_rules!` and procedural macros | `point!` | Two consequences matter in practice: - A struct with named fields lives only in the type namespace, so importing it beside a **function** called `Point` compiles: one binding is a type, the other a value. - A **tuple or unit struct** also defines a constructor in the value namespace, so importing a tuple struct `Point` beside a function `Point` is E0252 again, this time in the value namespace. - `std::fmt::Result` and `std::io::Result` are both type aliases, so importing both by name clashes even though they are type aliases rather than structs. ## The fixes 1. **Rename one import with `as`.** `use crate::svg::Point as SvgPoint;` binds `SvgPoint`. It is only a new name: `SvgPoint` and `crate::svg::Point` are one type and mix freely. 2. **Import the parent module.** `use crate::svg;` then `svg::Point` keeps the origin visible at every use site. The Book's idiom is to import functions through their parent module and types by full path, *except* when two types share a name, which is exactly this case; `fmt::Result` beside `io::Result` is its example. 3. **Combine both in one declaration.** `use crate::svg::{self, Point as SvgPoint};` binds the module `svg` and the alias `SvgPoint`. Inside braces, `self` names the parent, which must be a module, an enum or a trait. 4. **Import a trait as `_`.** When a trait is needed only for its methods and its name collides with something local, `use crate::shapes::Area as _;` brings the methods into scope without binding the name `Area`. ## Related clashes - An import and an **item declared in the same module** with one name is **E0255**: `use shapes::Point;` followed by `struct Point;`. The same `as` rename fixes it. - Names bound in different modules never clash: each module, and each block, has its own scope, so `render::Point` and `export::Point` can be different imports. - Nested braces cover several paths at once without new clashes, as long as each final binding name is unique: `use std::collections::{BTreeSet, hash_map::{self, HashMap}};`. ## Choosing a style in review - Rename with `as` when one of the two types dominates the module and the other appears in a few conversions; the alias reads naturally at the boundary. - Prefer the parent-module form when both are used heavily, so a reader never wonders which `Point` a signature means. - Avoid single-letter or vague aliases (`as P`); an alias is a name readers must learn, and `SvgPoint` explains itself. - When the clash comes from a dependency's type, renaming the import is local; renaming the dependency itself happens in `Cargo.toml` and is a Cargo concern. ## Scoping the import instead Because each block has its own scope, an import can also move to where it is needed. With `use crate::shapes::Point;` at module level, a conversion function may write `use crate::svg::Point;` inside its body; there, the inner import shadows the outer one and no E0252 arises, since the two bindings live in different scopes. It compiles, but `Point` now means different types a few lines apart, so reserve it for short functions and prefer an alias when readers would have to track which `Point` is meant. None of these fixes changes types or behaviour: they only decide which local names refer to which items.
- Why does `use std::fmt::Result; use std::io::Result;` also fail?Both are type aliases, and aliases occupy the type namespace like structs do, so the second import binds a name already taken: E0252. The Book's fix is `use std::fmt; use std::io;` and writing `fmt::Result` and `io::Result`, or renaming one with `as IoResult`.
- What happens when a module imports `shapes::Point` and also declares its own `struct Point`?rustc reports E0255: the import and the local item bind `Point` in the same namespace of one module. Rename the import (`use shapes::Point as ShapePoint;`). A glob import would instead be shadowed silently by the local struct, which is a different rule.
- When is `use some::Trait as _;` the right fix for a name clash?When you need the trait only so its methods resolve and its name collides with a local item or another import. The underscore import puts the trait in scope for method calls without binding its name, so `struct Area` and the trait `Area` can coexist.
- The second use silently replaces the first.
- E0252 fires only once the clashing name is used.
- An as alias creates a new type that cannot mix with the original.
- A struct and a function can never share a name in one module.
- Fixing the clash requires renaming one of the original types.