Rust

E0252 Duplicate Import Fix

A name can be bound only once in a namespace, and E0252 is the compiler refusing a second import binding for a name that already has one. This guide separates the shapes that produce it - a repeated import, a collision between two modules, two paths to one item, a repeated brace entry, a collision inside a block, and a duplicate inside a nested module - and matches each one to one of the only two repairs there are: remove the import that is not needed, or keep both items and rebind one of them with as.

Written November 23, 2026 · Tested with Rust 1.99.1 · 32 min read

By the end of this guide
  • The rule underneath: one binding per name, per namespace, per scope
  • How to read the diagnostic
  • The first failure: the same item imported twice
  • The second failure: the same name from two modules

Written November 23, 2026

Tested with Rust 1.99.1

What this guide is

Error E0252 is a name bound twice by imports. The error codes index defines it in one line: two items of the same name cannot be imported without rebinding one of the items under a new local name. A use declaration, in the Rust Reference's wording, creates one or more local name bindings synonymous with some other path - and a scope's namespace holds only one binding for a given name, so a second use that tries to bind the same name in the same namespace of the same module or block is E0252, however the second binding arrives.

The forms in this guide each reproduce E0252 with a small, complete program, and each brings the second binding in a different way. The same item imported twice on two lines. The same final name imported from two different modules, which hold two different items. The same item reached by two different paths - a re-export path and the defining module's path. A name repeated inside a single brace list. Two imports colliding inside a function body, where the scope in the diagnostic is a block, not a module. And a duplicate pair inside a nested module, where the collision is local to that module. In each family the failing program is small, the full compiler output is shown, and the fix is one of the two the error index prescribes: delete the redundant import when both bindings name the same item, or rebind one import with as when both items are needed - or, equivalently, drop one import and refer to that item by its full path at the use site.

Several neighboring results are not E0252 and are worked only in the traps and the decision summary. A glob import plus an explicit import of the same name compiles, because items and named imports are allowed to shadow names from glob imports. Two glob imports that both bring in the same name compile while the name is unused, and fail at the use site with E0659, not at the import. An explicit import of a name the prelude already supplies compiles. Two traits imported as _ compile, because an underscore import binds no name. A let binding with the same name as an import compiles, because a local variable is not an item binding in the module namespace. An import that collides with an item defined in the same module is E0255. And two definitions of the same type, with no import involved, are E0428.

Build every program in this guide with rustc --edition 2021. In the compiler transcripts below, the file names are the bare names shown on the command line. Several failing programs open with #![allow(unused_imports)]: without it the compiler would add an unused-import warning alongside the error, and the allow keeps the transcript to the one error the program is there to show. The allow changes no outcome - without it, every family below produces the same single E0252, apart from that warning.

The rule underneath: one binding per name, per namespace, per scope

A use declaration does not copy an item; it binds a local name to a path. The Reference puts the mechanism first: a use declaration creates local name bindings synonymous with some other path, may appear in modules and in blocks, and supports rebinding the target under a new local name with as, binding a list with braces, and binding all matching paths with the * glob. Every one of those forms ends in the same place - a name entered into a namespace of a scope - and the namespace is the unit the rule counts in. Rust keeps separate namespaces, so the diagnostic always names one: "must be defined only once in the type namespace of this module", "in the value namespace of this block", and so on. Two bindings collide only when name, namespace, and scope all match; the traps below are mostly shapes where one of the three differs.

That is why the same item imported twice is still an error. The rule is about bindings, not about items: use std::collections::HashMap; twice creates two bindings for HashMap, and the second is refused even though both point at the identical type - there is no exception for "the same thing again", and no deduplication of imports. It is also why the two repairs exhaust the possibilities. Deleting the second import leaves one binding, which is all a same-item duplicate ever needed. Rebinding with as - use bar::Baz as BarBaz; - gives the second item a different local name, so each name is bound once; the item itself keeps its original name in its own module, and only the local synonym changes. The third form the error index shows, referring to an item by its parent path at the use site (foo::baz in the index's example), is deletion plus a qualified use: one import is dropped, and the other item is reached by path instead of by a bound name.

Glob imports are the one form that does not follow the rule at import time, and the Reference states the exception directly: items and named imports are allowed to shadow names from glob imports in the same namespace - if there is a name already defined by another item in the same namespace, the glob import will be shadowed. So a glob never produces E0252 against an explicit import, in either order, and two globs that overlap produce no error at the import lines at all; the overlap surfaces only when the ambiguous name is used, and then as E0659. The prelude is the other non-binding source of names: prelude names are supplied implicitly, not bound by a use in your module, so an explicit import of a prelude name such as Option binds it once and compiles - there is no first binding for it to collide with.

How to read the diagnostic

The header is constant across the whole family: "the name X is defined multiple times", with X the local name both imports tried to bind. The span work below it identifies both bindings. One span marks the second import, annotated "X reimported here" - that is the line the compiler is refusing. A second span marks the first import, annotated "previous import of the type X here" or "previous import of the value X here" - the wording names the kind of thing imported: type for types, value for functions and constants, and the kind itself for the rest, as in "previous import of the trait X here" or "previous import of the module X here". Below both, a note states the rule with the scope filled in: "X must be defined only once in the type namespace of this module", or "in the value namespace of this block" when the imports sit inside a function or block. Reading the three together gives the whole defect: which name, which import came first, and which scope and namespace they collided in.

The help, when it appears, offers the as repair: "you can use as to change the binding name of the import", with a rewrite that renames the second import. That help appears in this guide's families where the two imports name different items - two modules, a block collision - because there renaming is a plausible intent. Where both imports name the same item or the same path, no help appears: renaming a duplicate of the same item would leave a pointless second name for one thing, and the repair the compiler leaves you to see is deletion. When no help appears and the header names a different verb - "redefined here" instead of "reimported here" - the second binding is a definition or an item, not an import, and the program belongs to the traps: E0255 or E0428, not this family.

The first failure: the same item imported twice

This is the plainest form: two identical use lines. The second line binds nothing new - same path, same item, same name - and is refused on its own, before any use of the name is considered:

Rust
#![allow(unused_imports)]
use std::collections::HashMap;
use std::collections::HashMap;
fn main() { let mut m: HashMap<String, i32> = HashMap::new(); m.insert("a".to_string(), 1); println!("{}", m["a"]); }
Terminal
$ rustc --edition 2021 fail_same_twice.rs -o fail_same_twice
error[E0252]: the name `HashMap` is defined multiple times
 --> fail_same_twice.rs:3:5
  |
2 | use std::collections::HashMap;
  |     ------------------------- previous import of the type `HashMap` here
3 | use std::collections::HashMap;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^ `HashMap` reimported here
  |
  = note: `HashMap` must be defined only once in the type namespace of this module

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0252`.

Note what is absent: no as help. Both bindings name the same item, so there is nothing to rename toward - a second name for the same HashMap would compile with as, as the traps show, but it would be a synonym nobody needs. The fix deletes the duplicate line; nothing else in the program changes:

Rust
use std::collections::HashMap;
fn main() { let mut m: HashMap<String, i32> = HashMap::new(); m.insert("a".to_string(), 1); println!("{}", m["a"]); }
Terminal
$ rustc --edition 2021 fix_same_twice.rs -o fix_same_twice
$ ./fix_same_twice
1

The output is 1, the value inserted under "a". In real code this family is usually a merge artifact or a paste - two edits each added the import the code needed. The review question is only whether the two lines are truly identical; if the paths differ at all, the program belongs to the third family instead, and the answer is still deletion, for a different reason given there.

The second failure: the same name from two modules

This is the error index's own example shape, with types in place of its struct and module. foo::Baz and bar::Baz are different types that happen to share a final name; importing both tries to bind Baz twice, and the compiler cannot let the bare name Baz in this module mean two types:

Rust
#![allow(unused_imports)]
mod foo { pub struct Baz(pub i32); }
mod bar { pub struct Baz(pub i32); }
use foo::Baz;
use bar::Baz;
fn main() { let x = Baz(7); println!("{}", x.0); }
Terminal
$ rustc --edition 2021 fail_two_modules.rs -o fail_two_modules
error[E0252]: the name `Baz` is defined multiple times
 --> fail_two_modules.rs:5:5
  |
4 | use foo::Baz;
  |     -------- previous import of the type `Baz` here
5 | use bar::Baz;
  |     ^^^^^^^^ `Baz` reimported here
  |
  = note: `Baz` 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
  |
5 | use bar::Baz as OtherBaz;
  |              +++++++++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0252`.

Here the as help does appear, because both items are real and both may be wanted. Deleting either import would also compile - the program uses only one Baz - but it would silently decide which type the bare name means, and the next use of the other type would fail as an unresolved name instead. The fix keeps both types and renames the second binding, so each type has exactly one local name:

Rust
mod foo { pub struct Baz(pub i32); }
mod bar { pub struct Baz(pub i32); }
use foo::Baz;
use bar::Baz as BarBaz;
fn main() { let x = Baz(7); let y = BarBaz(8); println!("{} {}", x.0, y.0); }
Terminal
$ rustc --edition 2021 fix_two_modules.rs -o fix_two_modules
$ ./fix_two_modules
7 8

The output is 7 8, one value from each type. The naming convention in the fix - prefixing the renamed import with its module, BarBaz - is the error index's own pattern (baz as quux in its example is arbitrary; foo_baz in its fixed example is the module-prefixed form). A name that says where the item came from stays readable when the two types appear side by side, which is exactly when this family occurs.

The third failure: the same item by two different paths

HashMap is defined in std::collections::hash_map and re-exported from std::collections, so both paths below name the same type. The import lines look different - different enough to survive a read-through - but the local name and the item are the same, and the binding rule counts the name, so the second import is refused exactly as if the lines were identical:

Rust
#![allow(unused_imports)]
use std::collections::HashMap;
use std::collections::hash_map::HashMap;
fn main() { let mut m: HashMap<String, i32> = HashMap::new(); m.insert("k".to_string(), 3); println!("{}", m["k"]); }
Terminal
$ rustc --edition 2021 fail_two_paths.rs -o fail_two_paths
error[E0252]: the name `HashMap` is defined multiple times
 --> fail_two_paths.rs:3:5
  |
2 | use std::collections::HashMap;
  |     ------------------------- previous import of the type `HashMap` here
3 | use std::collections::hash_map::HashMap;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `HashMap` reimported here
  |
  = note: `HashMap` must be defined only once in the type namespace of this module

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0252`.

Re-exports are what make this family common: a crate's public surface routinely offers a short path to an item defined deeper down, and one part of a file - or one contributor - imports the short path while another imports the defining path. The diagnostic gives no hint that the two paths converge; both spans say only that HashMap was imported twice. The check is to resolve both paths - if they land on the same item, this is a duplicate, not a collision, and renaming would create a second local name for one type, the outcome the first family warns against. The fix deletes one path - the shorter, re-export path is the one kept here, as the crate's intended public route - and keeps the other import out:

Rust
use std::collections::HashMap;
fn main() { let mut m: HashMap<String, i32> = HashMap::new(); m.insert("k".to_string(), 3); println!("{}", m["k"]); }
Terminal
$ rustc --edition 2021 fix_two_paths.rs -o fix_two_paths
$ ./fix_two_paths
3

The output is 3, the value inserted under "k". The same shape appears with a crate root re-export against a module path, and with crate:: paths against self:: or relative paths to one item - in every case the question is where the paths land, not how they are spelled.

The fourth failure: a name repeated in a brace list

A brace list is not several imports that happen to share a line; it is one use declaration creating several bindings, and repeating a name inside it creates the second binding inside that single declaration. The compiler points both spans at the same line - the first Thing as the previous import, the second as the reimport:

Rust
#![allow(unused_imports)]
mod a { pub struct Thing(pub i32); }
use a::{Thing, Thing};
fn main() { let t = Thing(5); println!("{}", t.0); }
Terminal
$ rustc --edition 2021 fail_brace_dup.rs -o fail_brace_dup
error[E0252]: the name `Thing` is defined multiple times
 --> fail_brace_dup.rs:3:16
  |
3 | use a::{Thing, Thing};
  |         -----  ^^^^^ `Thing` reimported here
  |         |
  |         previous import of the type `Thing` here
  |
  = note: `Thing` must be defined only once in the type namespace of this module

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0252`.

Brace lists grow by accretion - a name is added for a new use, in a list that already contains it a few entries earlier - and a long list hides the repetition better than two separate lines do. The fix is the list with the entry once; written here as a plain single import because only one name remains, though use a::{Thing}; with other names retained in the braces is the same repair when the list has more entries:

Rust
mod a { pub struct Thing(pub i32); }
use a::Thing;
fn main() { let t = Thing(5); println!("{}", t.0); }
Terminal
$ rustc --edition 2021 fix_brace_dup.rs -o fix_brace_dup
$ ./fix_brace_dup
5

The output is 5. A brace entry can also collide with a separate use line for the same name - the declaration forms do not matter, only the bindings they create - so when this diagnostic's two spans sit in different declarations, one of them a brace list, the repair is still to leave exactly one binding standing.

The fifth failure: a collision inside a function block

use declarations may appear in blocks, and a block is its own scope with its own namespaces. Two imports inside a function body that bind the same name collide there - the diagnostic's note says "of this block", not "of this module" - even though the same two imports in different functions, or one at module level and one in a block, would not collide at all:

Rust
#![allow(unused_imports)]
mod a { pub fn val() -> i32 { 1 } }
mod b { pub fn val() -> i32 { 2 } }
fn main() { use a::val; use b::val; println!("{}", val()); }
Terminal
$ rustc --edition 2021 fail_block_two_mods.rs -o fail_block_two_mods
error[E0252]: the name `val` is defined multiple times
 --> fail_block_two_mods.rs:4:29
  |
4 | fn main() { use a::val; use b::val; println!("{}", val()); }
  |                 ------      ^^^^^^ `val` reimported here
  |                 |
  |                 previous import of the value `val` here
  |
  = note: `val` must be defined only once in the value namespace of this block
help: you can use `as` to change the binding name of the import
  |
4 | fn main() { use a::val; use b::val as other_val; println!("{}", val()); }
  |                                    ++++++++++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0252`.

Two details mark this family. The namespace in the note is the value namespace - val is a function - where the earlier families collided in the type namespace; the rule is identical, only the namespace differs, and a name bound only in the type namespace - a braced struct, a trait, an enum, a type alias - and a function name spelled alike would not collide with each other at all. A unit or tuple struct is the exception: it also binds its constructor in the value namespace, so importing one spelled like a function does collide, in the value namespace. And the scope is the block: moving the second import into a nested block inside the function, or into a different function, would separate the bindings into different scopes and compile - which is sometimes the honest fix when each import serves only a small stretch of code. Here both functions are called in the same block, so the fix renames the second binding in place:

Rust
mod a { pub fn val() -> i32 { 1 } }
mod b { pub fn val() -> i32 { 2 } }
fn main() { use a::val; use b::val as b_val; println!("{} {}", val(), b_val()); }
Terminal
$ rustc --edition 2021 fix_block_two_mods.rs -o fix_block_two_mods
$ ./fix_block_two_mods
1 2

The output is 1 2, one result from each function. Block imports are where this error most often surprises, because the imports are local and temporary-feeling - written for one function, sometimes for one expression - but the one-binding rule applies inside a block exactly as it does at the top of a file.

The sixth failure: a duplicate inside a nested module

Scope is per module, not per file. The pair below sits inside mod inner, and the collision is reported against that module - the note again says "of this module", meaning inner - while the crate root around it has no HashMap binding at all and is not involved. The same pair at the root and inside inner would be two bindings in two scopes and would compile:

Rust
#![allow(unused_imports)]
mod inner {
    use std::collections::HashMap;
    use std::collections::HashMap;
    pub fn count() -> usize { let m: HashMap<String, i32> = HashMap::new(); m.len() }
}
fn main() { println!("{}", inner::count()); }
Terminal
$ rustc --edition 2021 fail_inner_module.rs -o fail_inner_module
error[E0252]: the name `HashMap` is defined multiple times
 --> fail_inner_module.rs:4:9
  |
3 |     use std::collections::HashMap;
  |         ------------------------- previous import of the type `HashMap` here
4 |     use std::collections::HashMap;
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^ `HashMap` reimported here
  |
  = note: `HashMap` must be defined only once in the type namespace of this module

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0252`.

This family matters for diagnosis rather than for repair: when the two spans are indented inside a mod, searching the file's top-level imports for the duplicate finds nothing, and the fix has to land in the module the spans point into. It is also the family that explains a common non-error - a file where HashMap is imported at the root and again inside a submodule is correct, because those are different scopes; only a second binding in the same module is refused. The fix deletes the inner duplicate:

Rust
mod inner {
    use std::collections::HashMap;
    pub fn count() -> usize { let m: HashMap<String, i32> = HashMap::new(); m.len() }
}
fn main() { println!("{}", inner::count()); }
Terminal
$ rustc --edition 2021 fix_inner_module.rs -o fix_inner_module
$ ./fix_inner_module
0

The output is 0, the length of the empty map count builds. The repair is the first family's deletion, one scope down - which is the general shape of this whole error: find the scope named in the note, count the bindings for the name inside it, and leave one.

The fixes in reach order

  • Both imports are the same line, or resolve to the same item by different paths: a duplicate, not a collision - delete one import. Never rename; a second name for one item is clutter the next reader must untangle.
  • Both imports are needed and name different items with the same final name: keep both, rebind one with as, and name the alias for its origin - BarBaz, foo_baz - not for its position (Baz2).
  • One item is used only once or twice: delete its import and use the full path at the use site instead - the error index's third form - so the bare name keeps a single meaning.
  • The repeated name sits in a brace list: leave the entry once; check the rest of the file for a second declaration binding the same name outside the braces.
  • The collision is in a block and each import serves a different stretch of code: consider separating the imports into nested blocks before renaming - scope separation is the repair that keeps both bare names.
  • The spans are inside a mod: fix that module's imports, not the file's - and do not delete a same-named import in a different module or at the root, which is a different scope and not part of the defect.
  • In every case: after the fix, exactly one binding for the name stands in the named namespace of the named scope - count them, do not eyeball them.

Traps

  • Renaming one import with as compiles - it is the fix, not a family. Two different items with the same final name coexist as soon as one binding carries a different local name:
Rust
mod foo { pub struct Baz(pub i32); }
mod bar { pub struct Baz(pub i32); }
use foo::Baz as FooBaz;
use bar::Baz;
fn main() { let x = FooBaz(1); let y = Baz(2); println!("{} {}", x.0, y.0); }
Terminal
$ rustc --edition 2021 probe_alias.rs -o probe_alias
$ ./probe_alias
1 2
Trap
The output is 1 2. The same form also compiles when both imports name the same item - use std::collections::HashMap as Map2; alongside the plain import - which is legal and, for one item, pointless: legality is not a reason to keep a synonym. Rename when the items differ; delete when they do not. - A glob import plus an explicit import of the same name compiles - the explicit import wins. The Reference states the rule: items and named imports are allowed to shadow names from glob imports in the same namespace, and if a name is already defined by another item in the same namespace, the glob import will be shadowed. So Thing below is unambiguously a::Thing by the explicit import, and the glob still supplies label:
Rust
mod a { pub struct Thing(pub i32); pub fn label() -> i32 { 10 } }
use a::*;
use a::Thing;
fn main() { let t = Thing(4); println!("{} {}", t.0, label()); }
Terminal
$ rustc --edition 2021 probe_glob_explicit.rs -o probe_glob_explicit
$ ./probe_glob_explicit
4 10

The output is 4 10. This is the standard way to disambiguate a glob overlap - adding the explicit import is a repair, never an E0252 - and it works for a trait name as for a type: a glob that brings in a trait plus an explicit import of that same trait compiles the same way. Do not remove either line to "fix" an error that is not there. - An explicit import of a prelude name compiles. Option is in the standard prelude, supplied implicitly to every module - but implicitly supplied is not bound by an import, so the explicit use below is the name's first and only import binding:

Rust
#![allow(unused_imports)]
use std::option::Option;
fn main() { let x: Option<i32> = Some(9); println!("{:?}", x); }
Terminal
$ rustc --edition 2021 probe_prelude.rs -o probe_prelude
$ ./probe_prelude
Some(9)

The output is Some(9). The import is redundant - the name was already usable - and a lint, not this error, is the most it can draw. The same holds for Vec, String, and the other prelude names: shadowing the prelude takes an item definition or a binding in a nearer scope, not a mere import of the prelude's own item. - Two traits imported as _ compile, because an underscore import binds no name. The Reference describes the form directly: items can be imported without binding to a name by using an underscore, use path as _, particularly useful to import a trait so its methods may be used without importing the trait's symbol - for example if the trait's symbol may conflict with another symbol. Two of them cannot collide, since neither enters a name into any namespace:

Rust
#![allow(unused_imports, dead_code)]
mod a { pub trait T { fn t(&self) -> i32 { 1 } } }
mod b { pub trait T { fn t(&self) -> i32 { 2 } } }
use a::T as _;
use b::T as _;
struct S;
impl a::T for S {}
fn main() { println!("{}", S.t()); }
Terminal
$ rustc --edition 2021 probe_underscore.rs -o probe_underscore
$ ./probe_underscore
1

The output is 1, from a::T's default method - the trait S implements. When two same-named traits are both needed only for their methods, as _ is a better repair than inventing two local trait names nobody will ever write in a path. - A let binding with the same name as an import compiles. A local variable lives in the value scope of the block, not in the module's item namespaces, and it shadows the imported function for the rest of the block:

Rust
#![allow(unused_imports, dead_code)]
mod a { pub fn val() -> i32 { 1 } }
use a::val;
fn main() { let val = 42; println!("{}", val); }
Terminal
$ rustc --edition 2021 probe_let_shadow.rs -o probe_let_shadow
$ ./probe_let_shadow
42

The output is 42, the local, not 1, the imported function. Shadowing by let is a statement-level mechanism and follows its own rules - a second let val would shadow again, just as legally. E0252 is only ever about two item bindings - two imports, in this guide's families - never about a variable that happens to share a name with one. - Two glob imports that overlap compile while the shared name is unused. A glob binds no single name at the import line in the way an explicit import does, so two globs bringing in the same Thing produce no diagnostic at all until the name is used:

Rust
#![allow(unused_imports, dead_code)]
mod a { pub struct Thing(pub i32); }
mod b { pub struct Thing(pub i32); }
use a::*;
use b::*;
fn main() { println!("unused"); }
Terminal
$ rustc --edition 2021 probe_glob_glob_unused.rs -o probe_glob_glob_unused
$ ./probe_glob_glob_unused
unused

The output is unused. The overlap is latent, not absent: it is waiting for the first use of Thing, which is the next trap. A build that passes with overlapping globs has not been shown to be free of the collision - only that nothing has asked the ambiguous question yet. - Using a name brought in by two glob imports is E0659, not E0252. The failure sits at the use site, its header says the name "is ambiguous", and its notes name both glob imports as candidates - there is no "reimported here" span, because neither import was refused:

Rust
#![allow(unused_imports)]
mod a { pub struct Thing(pub i32); }
mod b { pub struct Thing(pub i32); }
use a::*;
use b::*;
fn main() { let t = Thing(1); println!("{}", t.0); }
Terminal
$ rustc --edition 2021 probe_glob_glob_used.rs -o probe_glob_glob_used
error[E0659]: `Thing` is ambiguous
 --> probe_glob_glob_used.rs:6:21
  |
6 | fn main() { let t = Thing(1); println!("{}", t.0); }
  |                     ^^^^^ ambiguous name
  |
  = note: ambiguous because of multiple glob imports of a name in the same module
note: `Thing` could refer to the tuple struct imported here
 --> probe_glob_glob_used.rs:4:5
  |
4 | use a::*;
  |     ^^^^
  = help: consider adding an explicit import of `Thing` to disambiguate
note: `Thing` could also refer to the tuple struct imported here
 --> probe_glob_glob_used.rs:5:5
  |
5 | use b::*;
  |     ^^^^
  = help: consider adding an explicit import of `Thing` to disambiguate

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0659`.

The error codes index defines E0659 as an item usage that is ambiguous, generally appearing when two items with the same name are imported into a module. Its repair is the glob trap above in reverse: add an explicit import of the name you mean - the explicit import shadows both globs - or qualify the use with the module path. Deleting a glob also works, at the cost of every other name it supplied. - An import colliding with an item defined in the same module is E0255, not E0252. The header text is identical - "the name HashMap is defined multiple times" - so the code and the second span decide: here the second binding is a definition, annotated "redefined here", not an import:

Rust
#![allow(unused_imports)]
use std::collections::HashMap;
struct HashMap;
fn main() { println!("ok"); }
Terminal
$ rustc --edition 2021 probe_import_vs_item_e0255.rs -o probe_import_vs_item_e0255
error[E0255]: the name `HashMap` is defined multiple times
 --> probe_import_vs_item_e0255.rs:3:1
  |
2 | use std::collections::HashMap;
  |     ------------------------- previous import of the type `HashMap` here
3 | struct HashMap;
  | ^^^^^^^^^^^^^^^ `HashMap` redefined here
  |
  = note: `HashMap` 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
  |
2 | use std::collections::HashMap as OtherHashMap;
  |                               +++++++++++++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0255`.

The error codes index defines E0255 as importing a value whose name is the same as another value defined in the module, and its repairs mirror E0252's - alias the import, or refer to the imported item by its parent path - with one addition E0252 never offers: renaming the local item is also on the table, since the item is yours in a way an imported path is not. - Two definitions of the same name, with no import at all, are E0428, not E0252. Both spans are definitions - "previous definition" and "redefined here" - and neither mentions an import:

Rust
struct Bar;
struct Bar;
fn main() { println!("ok"); }
Terminal
$ rustc --edition 2021 probe_dup_def_e0428.rs -o probe_dup_def_e0428
error[E0428]: the name `Bar` is defined multiple times
 --> probe_dup_def_e0428.rs:2:1
  |
1 | struct Bar;
  | ----------- previous definition of the type `Bar` here
2 | struct Bar;
  | ^^^^^^^^^^^ `Bar` redefined here
  |
  = note: `Bar` must be defined only once in the type namespace of this module

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0428`.

The error codes index defines E0428 as a type or module that has been defined more than once, and its repair is the definition-side one: verify the name was not misspelled, and remove or rename the duplicated definition. The three codes share a header sentence by design - the name is defined multiple times in all of them - and are told apart entirely by what the two bindings are: import and import is E0252, import and definition is E0255, definition and definition is E0428.

Decision summary

  • The header says "the name X is defined multiple times", the code is E0252, and both spans are imports of the same path (E0252): a duplicate - delete one import.
  • The header says the same, the code is E0252, and the two imports name different items with the same final name (E0252): keep both items, rebind one with as under an origin-named alias, or drop one import and qualify its uses by path.
  • The header says the same, the code is E0252, and the two paths differ but resolve to the same item (E0252): a duplicate by re-export - delete one path, keeping the crate's intended public path.
  • The header says the same, the code is E0252, and both spans sit on one brace-list line (E0252): leave the entry once.
  • The header says the same, the code is E0252, and the note names a block instead of a module (E0252): the collision is local to that block - rename one binding there, or separate the imports into nested blocks.
  • The header says the same, the code is E0252, and both spans sit inside a mod (E0252): fix that module's imports; same-named imports in other scopes are not part of the defect.
  • A glob import and an explicit import of the same name compile: no error - the explicit import shadows the glob for that name.
  • An explicit import of a prelude name, two traits imported as _, or a let sharing an import's name compile: no error - none of them creates a second item binding in the namespace.
  • Two glob imports of the same name compile while the name is unused: no error yet - the collision is latent and surfaces at the first use.
  • The header says "X is ambiguous" at a use site, with both glob imports named as candidates (E0659): add an explicit import of the intended item, or qualify the use by path.
  • The header says "the name X is defined multiple times" and the second span says "redefined here" against a local item (E0255): an import collided with a definition - alias the import, qualify it, or rename the local item.
  • The header says the same and both spans are definitions (E0428): a duplicated definition - remove or rename one definition; no import is involved.

Checklist

  • Both spans read, not just the header: import-and-import is E0252, import-and-definition is E0255, definition-and-definition is E0428 - the header sentence is the same for all three.
  • The scope and namespace taken from the note: module or block, type or value - the fix lands in exactly that scope, and bindings in other scopes or namespaces are left alone.
  • The two paths resolved before choosing a repair: same item means delete, different items means rename or qualify - never rename a duplicate, never delete a collision's only import of a needed item.
  • If renamed, the alias names the origin (BarBaz, foo_baz), and every use of that item in the scope uses the alias - a missed use now resolves to the other item or fails as unresolved.
  • If deleted, the whole file checked for the same name bound again - a brace list elsewhere, a second module with its own pair - since one duplicate usually means the import was added twice in more than one place.
  • Glob overlaps checked separately: no E0252 from a glob is expected, so a glob-heavy file is checked for latent E0659 by confirming every glob-supplied name in use resolves to one glob or to an explicit import.
  • Traits needed only for their methods imported as _ where two same-named traits meet, instead of inventing local names for traits that are never named in a path.
  • After the repair, the program compiled again and run: the fix is finished only when the name in the output comes from the item the program was written to use - in a collision fix, both items exercised, not just the one that kept the bare name.

Sources

  • Rust, Error codes index: E0252 - the definition of the error: two items of the same name cannot be imported without rebinding one of the items under a new local name - with the two-module baz example, the fix of rebinding one import with as, and the alternative of referencing an item with its parent path at the use site.
  • The Rust Reference, Use declarations - a use declaration creates one or more local name bindings synonymous with some other path, may appear in modules and blocks, and supports rebinding with as, brace lists, and the * glob; items and named imports are allowed to shadow names from glob imports in the same namespace, so a glob import is shadowed where another item defines the name; and items can be imported without binding to a name with use path as _, particularly useful for a trait whose symbol may conflict with another symbol.
  • Rust, Error codes index: E0255 - the neighboring import error: a value cannot be imported whose name is the same as another value defined in the module - the code an import colliding with a local definition produces, repaired by aliasing the import or referring to the item by its parent.
  • Rust, Error codes index: E0428 - the neighboring definition error: a type or module has been defined more than once - the code two definitions of one name produce, with no import involved.
  • Rust, Error codes index: E0659 - the neighboring glob error: an item usage is ambiguous, generally appearing when two items with the same name are imported into a module - the code a use of a name supplied by two glob imports produces, at the use site rather than at either import.

Keep Exploring