Rust

E0428 Duplicate Definition Fix

A name can be claimed only once in each namespace of a scope, and E0428 is the compiler refusing the second claim. This guide separates the shapes that produce it - duplicates among functions, types, modules, constants, traits, constructors, and enum variants - and matches each one to the same repair in different clothing: give one of the two definitions a different name, or a different home.

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

By the end of this guide
  • The rule underneath: one claim per name, per namespace, per scope
  • How to read the diagnostic
  • The first failure: two functions with the same name
  • The second failure: two structs with the same name

Written November 23, 2026

Tested with Rust 1.99.1

What this guide is

Error E0428 is a name claimed twice. The error codes index defines it in one line: a type or module has been defined more than once. The compiler's own wording is wider than that one line suggests - the header reads "the name X is defined multiple times", and the families below show the claim being refused for functions, constants, and enum variants as well as for types and modules. In every case the defect has the same three parts: two definitions, the same name, and the same namespace in the same scope. Remove any one of the three and the error goes with it, which is why the fixes in this guide are only ever renames or moves.

The forms in this guide each reproduce E0428 with a small, complete program, and each claims a name twice in a different way. Two functions with the same name. Two structs with the same name. A struct and an enum with the same name. Two type aliases with the same name. Two modules with the same name. Two constants with the same name, and a constant and a static with the same name. Two traits with the same name. A unit struct and a constant fighting over the constructor's name. And two variants of one enum with the same name. In each family the failing program is small, the full compiler output is shown, and the fix renames the second definition - the error index's own prescription is to verify you did not misspell the name, or remove or rename the duplicated one.

Several neighboring results are not E0428 and are worked only in the traps and the decision summary. The same name in two different namespaces - a named-field struct and a constant, a type alias and a constant, a tuple struct and a macro - compiles, because the claims land in different namespaces. The same type name defined in two sibling modules, or again inside an inner block, compiles, because the claims land in different scopes. A second let of the same name compiles, because a binding shadows rather than redefines. Importing the same name twice from two modules is E0252, not E0428 - defining twice is this guide, importing twice is the neighboring one. A duplicate method inside one impl is E0592, a second implementation of the same trait for the same type is E0119, and a duplicate field in a struct is E0124.

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.

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

A declaration introduces a name, and names are segregated into namespaces by the kind of entity they refer to. The Rust Reference on namespaces states the point of the segregation directly: namespaces allow the occurrence of a name in one namespace to not conflict with the same name in another namespace. Three namespaces carry almost everything in this guide. The type namespace holds module declarations, structs, unions, enums and their variants, traits, and type aliases. The value namespace holds function declarations, constant and static items, and - the entry that produces the eighth family - struct constructors and enum variant constructors. The macro namespace holds macro_rules declarations and the macro kinds. Lifetimes and loop labels have their own namespaces, separate again.

E0428 is what the compiler reports when a second declaration tries to enter a name that a first declaration already holds in the same namespace of the same module or scope. The note under every transcript in this guide states the rule in its local form - the name "must be defined only once in the value namespace of this module", "in the type namespace of this module", or, for variants, "in the type namespace of this enum". Kind does not matter inside a namespace: a struct and an enum are different kinds of item, but both live in the type namespace, so one name cannot name both in one module. A constant and a static are different kinds of item, but both live in the value namespace, so the same holds. What decides the collision is never what the two definitions are - it is which namespace each one claims in, and where.

Two consequences of the rule explain the shapes that do not fail. First, the same name can be claimed once in each namespace at the same time: a named-field struct claims its name only in the type namespace - it has no constructor in the value namespace, because its instances are built with a struct literal - so a constant of the same name, claiming the value namespace, does not collide with it. A tuple struct is the opposite case: it claims its name in the type namespace and its constructor claims the same name in the value namespace, so the name is spent in both, and a constant of that name collides with the constructor - the eighth family. A macro_rules name claims only the macro namespace, so a tuple struct and a macro can share a name outright. Second, the claim is per scope: a module is a new scope, and so is a block, so the same name defined inside a nested module or an inner block is a separate claim that never meets the outer one. The traps work both consequences.

The fix follows from the rule rather than from the diagnostic's wording. Since the defect is two claims to one name in one namespace of one scope, the repair is to withdraw one claim: rename one definition - and every use of it - or move one definition into a module of its own, where its claim is the only one. Deleting one definition is the third form of the same move, and is the right one when the duplicate is a copy-paste accident rather than two genuinely different things that happen to share a name. There is no attribute, ordering trick, or visibility change that lets both claims stand: order does not matter, because a module's items are resolved as a set, not in sequence - the second definition in file order is the one the diagnostic marks as the redefinition, but swapping the two definitions only swaps which one is marked.

How to read the diagnostic

The header is constant across the whole family: "the name X is defined multiple times", with the colliding name in backticks. The span work below it names both definitions. An underline beneath the first definition reads "previous definition of the value answer here" - or "of the type", "of the module", "of the trait", matching the kind of the first definition - and a caret span on the second definition reads "answer redefined here". The note that follows is the part to act on: it names the namespace and the scope the two definitions are fighting over, in the form "answer must be defined only once in the value namespace of this module". Reading the three together gives the whole defect: which name, which two definitions hold it, and which namespace makes them collide.

That note is also the fastest way to tell this error from its neighbors. When the note names a namespace of a module, the program is in one of this guide's families and one definition must be renamed or moved. When the header instead says the name was "reimported here" and the code is E0252, neither definition is in this file twice - the name arrived twice through use declarations, and the repair is an as alias on one import, worked in the traps. And when there is no E0428 header at all - a duplicate method reports E0592 with the header "duplicate definitions with name", a duplicate field reports E0124 - the collision is inside a single item's own list, not between two items in a namespace, and renaming a field or a method, not an item, is the repair.

The first failure: two functions with the same name

Functions live in the value namespace, so two functions of the same name in one module are two claims to one value-namespace name. Nothing about the signatures separates them - Rust has no function overloading, and a second answer with a different body, or even a different signature, is a redefinition rather than an overload:

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

error: aborting due to 1 previous error

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

The diagnostic calls both definitions "the value answer", which is the namespace speaking: in the value namespace the two functions are the same kind of claim. The usual real-world cause is a merge or a paste that leaves an old and a new version of a helper in one file, and there the repair is deletion of the stale one. When both functions are genuinely wanted, the second takes the name that says how it differs:

Rust
fn answer() -> i32 { 1 }
fn answer_alt() -> i32 { 2 }
fn main() { println!("{} {}", answer(), answer_alt()); }
Terminal
$ rustc --edition 2021 fix_fn_fn.rs -o fix_fn_fn
$ ./fix_fn_fn
1 2

The output is 1 2, both functions callable under their own names. The same collision inside an impl block is not this family - two methods of the same name in one impl report E0592, worked in the traps, because an impl's method list is not a module namespace.

The second failure: two structs with the same name

This is the error index's own example shape. A unit struct claims its name in the type namespace - and, being a unit struct, its constructor claims the same name in the value namespace too - so a second struct of the same name has nowhere to stand:

Rust
struct Bar;
struct Bar;
fn main() { let _b = Bar; }
Terminal
$ rustc --edition 2021 fail_struct_struct.rs -o fail_struct_struct
error[E0428]: the name `Bar` is defined multiple times
 --> fail_struct_struct.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`.

Here the diagnostic calls the first definition "the type Bar" where the first family said "the value" - the kind word in the span label follows the namespace the span is reporting on, and for a struct that is the type namespace. The error index's own fix renames the second definition, and the fix here follows it exactly:

Rust
struct Bar;
struct Bar2;
fn main() { let _a = Bar; let _b = Bar2; println!("both defined"); }
Terminal
$ rustc --edition 2021 fix_struct_struct.rs -o fix_struct_struct
$ ./fix_struct_struct
both defined

The output is both defined, both unit structs constructed. In real code the second struct usually deserves a descriptive name rather than a numbered one - the rename is the repair either way, and every construction and annotation of the second struct takes the new name with it.

The third failure: a struct and an enum with the same name

Different kinds of item do not escape the rule when they share a namespace. A struct and an enum both claim their names in the type namespace, so one module cannot hold a struct Thing and an enum Thing, even though one is built with a literal and the other with variants:

Rust
struct Thing { value: i32 }
enum Thing { A }
fn main() { let _t = Thing { value: 1 }; }
Terminal
$ rustc --edition 2021 fail_struct_enum.rs -o fail_struct_enum
error[E0428]: the name `Thing` is defined multiple times
 --> fail_struct_enum.rs:2:1
  |
1 | struct Thing { value: i32 }
  | ------------ previous definition of the type `Thing` here
2 | enum Thing { A }
  | ^^^^^^^^^^ `Thing` redefined 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 E0428`.

This is the family that surprises programmers arriving from languages where a type's kind is part of its identity. In Rust the type namespace is flat across kinds: structs, enums, unions, traits, and aliases all draw names from the one pool, so converting a struct into an enum - a common refactor, when the struct grows variants - requires removing or renaming the struct in the same edit, not merely adding the enum beside it. The fix renames the enum to say it is the kind, not the thing:

Rust
struct Thing { value: i32 }
enum ThingKind { A }
fn main() { let t = Thing { value: 1 }; let k = ThingKind::A; println!("{} {}", t.value, match k { ThingKind::A => "a" }); }
Terminal
$ rustc --edition 2021 fix_struct_enum.rs -o fix_struct_enum
$ ./fix_struct_enum
1 a

The output is 1 a, the struct's field and the enum's variant both reachable. The alternative repair - moving the enum into a submodule - works by the per-scope rule, and is the better shape when the two types belong to different concerns that merely reached for the same word.

The fourth failure: two type aliases with the same name

A type alias claims its name in the type namespace, exactly as the type it aliases does. Two aliases of the same name in one module collide even when they alias different types - the name is the claim, and what it would have referred to never gets a chance to matter:

Rust
type Count = i32;
type Count = i64;
fn main() { let _c: Count = 1; }
Terminal
$ rustc --edition 2021 fail_alias_alias.rs -o fail_alias_alias
error[E0428]: the name `Count` is defined multiple times
 --> fail_alias_alias.rs:2:1
  |
1 | type Count = i32;
  | ----------------- previous definition of the type `Count` here
2 | type Count = i64;
  | ^^^^^^^^^^^^^^^^^ `Count` redefined here
  |
  = note: `Count` 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`.

An alias is transparent - it names the same type, it does not make a new one - so two aliases of one name cannot be kept apart by their right-hand sides, and there is no sense in which the second is a refinement of the first. A struct and an alias of the same name collide the same way, for the same reason: a struct Foo followed by type Foo = i32; produces this same error, with the note naming the type namespace of the module, because both definitions claim in that namespace. The fix gives the wider alias the name that says what widened:

Rust
type Count = i32;
type WideCount = i64;
fn main() { let a: Count = 1; let b: WideCount = 2; println!("{} {}", a, b); }
Terminal
$ rustc --edition 2021 fix_alias_alias.rs -o fix_alias_alias
$ ./fix_alias_alias
1 2

The output is 1 2. Where the duplicate alias is a leftover from changing a type's width, the repair is the opposite one: delete the stale alias and let the surviving name refer to the new type everywhere - an alias rename and an alias deletion are both withdrawals of one claim, and which is right depends on whether two types or one are actually in play.

The fifth failure: two modules with the same name

Module declarations live in the type namespace - the Reference lists them there - so two modules of the same name in one parent are two claims to one type-namespace name, and the second module's contents do not merge into the first:

Rust
mod util { pub fn a() -> i32 { 1 } }
mod util { pub fn b() -> i32 { 2 } }
fn main() { println!("{}", util::a()); }
Terminal
$ rustc --edition 2021 fail_mod_mod.rs -o fail_mod_mod
error[E0428]: the name `util` is defined multiple times
 --> fail_mod_mod.rs:2:1
  |
1 | mod util { pub fn a() -> i32 { 1 } }
  | -------- previous definition of the module `util` here
2 | mod util { pub fn b() -> i32 { 2 } }
  | ^^^^^^^^ `util` redefined here
  |
  = note: `util` 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 span label here says "the module util" - the third kind word in this guide's transcripts, after "the value" and "the type" - while the note still names the type namespace, which is where module names live. Programmers arriving from languages with reopenable namespaces or partial classes expect the two util blocks to combine into one module holding both functions; Rust modules do not reopen, and the expectation is exactly what this family refuses. When merging was the intent, the repair is one module holding both items. When two modules were intended, the second takes its own name:

Rust
mod util { pub fn a() -> i32 { 1 } }
mod extra { pub fn b() -> i32 { 2 } }
fn main() { println!("{} {}", util::a(), extra::b()); }
Terminal
$ rustc --edition 2021 fix_mod_mod.rs -o fix_mod_mod
$ ./fix_mod_mod
1 2

The output is 1 2, one function reached through each module. Note the contrast the per-scope rule draws: a module named util inside another module does not collide with a top-level util, because the inner declaration claims its name in the inner module's scope - nesting is the move form of the fix, renaming is the name form.

The sixth failure: two constants with the same name - and a constant and a static

Constants and statics both live in the value namespace. Two constants of the same name in one module are two claims to one value-namespace name, and the values do not matter - the second is a redefinition even if it repeats the first's value exactly:

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

error: aborting due to 1 previous error

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

The cross-kind form of the same family is a constant and a static of one name. A const and a static differ in what they are - a constant is inlined at each use, a static has one fixed address - but both claim in the value namespace, so the difference in kind does not separate their names:

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

error: aborting due to 1 previous error

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

This is the value-namespace mirror of the struct-and-enum family: inside one namespace, kind is not identity. The same holds across the rest of the value namespace - a function and a constant of the same name collide the same way, as a function value followed by a constant value produces this same error with the note naming the value namespace. The fix for the constant pair renames the alternative limit, and for the mixed pair renames the static:

Rust
const LIMIT: i32 = 10;
const ALT_LIMIT: i32 = 20;
fn main() { println!("{} {}", LIMIT, ALT_LIMIT); }
Terminal
$ rustc --edition 2021 fix_const_const.rs -o fix_const_const
$ ./fix_const_const
10 20
Rust
const VALUE: i32 = 1;
static OTHER_VALUE: i32 = 2;
fn main() { println!("{} {}", VALUE, OTHER_VALUE); }
Terminal
$ rustc --edition 2021 fix_const_static.rs -o fix_const_static
$ ./fix_const_static
1 2

The outputs are 10 20 and 1 2. Converting a const to a static - or the reverse - is a common reason this family appears mid-refactor: the item changes kind in place, a second copy is added during the edit, and the name ends up claimed by both forms at once. Finish the conversion by removing the old form, rather than renaming, when only one item is meant to survive.

The seventh failure: two traits with the same name

Traits live in the type namespace, alongside structs and enums. Two traits of the same name in one module collide even with identical method lists - the collision is between the two claims to the name, and the trait's contents are never compared:

Rust
trait Draw { fn draw(&self); }
trait Draw { fn draw(&self); }
fn main() {}
Terminal
$ rustc --edition 2021 fail_trait_trait.rs -o fail_trait_trait
error[E0428]: the name `Draw` is defined multiple times
 --> fail_trait_trait.rs:2:1
  |
1 | trait Draw { fn draw(&self); }
  | ---------- previous definition of the trait `Draw` here
2 | trait Draw { fn draw(&self); }
  | ^^^^^^^^^^ `Draw` redefined here
  |
  = note: `Draw` 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 span label says "the trait Draw" - a fourth kind word, still reporting on the type namespace the note names. A duplicated trait usually arrives the same way a duplicated function does, as a paste or merge leftover; but it also arrives when two genuinely different capabilities reach for one generic word - drawing to a screen and drawing from a deck - and there the rename is doing real design work, because the trait name is what implementations and bounds will say everywhere. The fix renames the second capability:

Rust
trait Draw { fn draw(&self) -> i32; }
trait Sketch { fn sketch(&self) -> i32; }
struct Dot;
impl Draw for Dot { fn draw(&self) -> i32 { 1 } }
impl Sketch for Dot { fn sketch(&self) -> i32 { 2 } }
fn main() { let d = Dot; println!("{} {}", d.draw(), d.sketch()); }
Terminal
$ rustc --edition 2021 fix_trait_trait.rs -o fix_trait_trait
$ ./fix_trait_trait
1 2

The output is 1 2, one result from each trait's method. Keep this family separate from the second implementation of one trait for one type: that program defines the trait once, implements it twice, and reports E0119, worked in the traps - the collision there is between implementations, not between names.

The eighth failure: a unit struct and a constant fight over the constructor

A unit struct claims its name twice over: once in the type namespace for the type, and once in the value namespace for its constructor, since Marker alone is an expression that builds the value. A constant of the same name claims the value namespace too, and it is the constructor's claim - not the type's - that it collides with:

Rust
struct Marker;
const Marker: i32 = 1;
fn main() { let _m = Marker; }
Terminal
$ rustc --edition 2021 fail_unitstruct_const.rs -o fail_unitstruct_const
error[E0428]: the name `Marker` is defined multiple times
 --> fail_unitstruct_const.rs:2:1
  |
1 | struct Marker;
  | -------------- previous definition of the value `Marker` here
2 | const Marker: i32 = 1;
  | ^^^^^^^^^^^^^^^^^^^^^^ `Marker` redefined here
  |
  = note: `Marker` must be defined only once in the value namespace of this module

error: aborting due to 1 previous error

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

The tell is in the first span label: the struct is described as "previous definition of the value Marker" - the compiler is pointing at the struct's constructor claim, not its type claim, because the constant fights in the value namespace. A tuple struct behaves the same way, since it too has a constructor in the value namespace; a named-field struct does not, which is why the traps can pair one with a constant of the same name. This family is the practical proof that a struct's name is not a single claim. The fix renames the constant, leaving the struct's two claims intact:

Rust
struct Marker;
const MARKER_VALUE: i32 = 1;
fn main() { let _m = Marker; println!("{}", MARKER_VALUE); }
Terminal
$ rustc --edition 2021 fix_unitstruct_const.rs -o fix_unitstruct_const
$ ./fix_unitstruct_const
1

The output is 1, the renamed constant's value. When a name seems to collide with a struct "for no reason", check which of the struct's claims the note's namespace points at - a value-namespace note against a struct means the constructor is one of the two claimants, and something value-like - a constant, a static, a function - is the other.

The ninth failure: two variants of one enum with the same name

An enum's variants claim their names in the type namespace of the enum itself - the note's scope reads "of this enum" rather than "of this module" - so two variants of one enum cannot share a name, even though the same variant name in two different enums is unremarkable:

Rust
enum Direction { North, North }
fn main() { let _d = Direction::North; }
Terminal
$ rustc --edition 2021 fail_variant.rs -o fail_variant
error[E0428]: the name `North` is defined multiple times
 --> fail_variant.rs:1:25
  |
1 | enum Direction { North, North }
  |                  -----  ^^^^^ `North` redefined here
  |                  |
  |                  previous definition of the type `North` here
  |
  = note: `North` must be defined only once in the type namespace of this enum

error: aborting due to 1 previous error

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

Both spans sit on the one line, because the whole scope is the enum's variant list. The variant list is a namespace of its own in miniature: within it, a name identifies one variant, and a duplicate would make Direction::North ambiguous between two different values - which may carry different payloads, so the ambiguity is not merely cosmetic. The fix names the second variant for the direction it actually is:

Rust
enum Direction { North, South }
fn main() { let a = Direction::North; let b = Direction::South; let name = |d: Direction| match d { Direction::North => "north", Direction::South => "south" }; println!("{} {}", name(a), name(b)); }
Terminal
$ rustc --edition 2021 fix_variant.rs -o fix_variant
$ ./fix_variant
north south

The output is north south. The per-scope rule is what makes the same variant name in a second enum legal: each enum's variant list is its own scope, so North in Direction and North in some other enum are separate claims that never meet - it is only the duplicate inside one list that this family refuses.

The fixes in reach order

  • Two functions of one name in one module: delete the stale one, or rename the one that differs - there is no overloading to fall back on.
  • Two types of one name - structs, or a struct against an enum, an alias, or a trait: rename one type, or move one into a submodule; kind does not separate names inside the type namespace.
  • Two modules of one name: merge them into one module if one module was meant - modules do not reopen - or rename the second.
  • Two value items of one name - constants, a constant against a static, a function against a constant: rename one; kind does not separate names inside the value namespace either.
  • A value item against a struct of the same name, with a value-namespace note: the struct's constructor is the other claimant - rename the value item, or make the struct named-field if its constructor is not needed.
  • Two variants of one name in one enum: rename the second variant; the same variant name in another enum needs nothing.
  • In every case: the rename reaches every use - constructions, annotations, paths, bounds, and patterns - and the program is recompiled after it, because a missed use is a fresh error at that use, not a leftover of this one.

Traps

  • A named-field struct and a constant of the same name compile - they claim in different namespaces. The struct claims X only in the type namespace, because a named-field struct has no value-namespace constructor - its values are built with the X { ... } literal - and the constant claims X in the value namespace, so the two claims never meet:
Rust
struct X { value: i32 }
const X: i32 = 7;
fn main() { let s = X { value: 1 }; println!("{} {}", s.value, X); }
Terminal
$ rustc --edition 2021 trap_struct_const.rs -o trap_struct_const
$ ./trap_struct_const
1 7
Trap
The output is 1 7: in the annotation-and-literal position X is the type, and in the expression position X is the constant, and the namespace of the position decides which is meant. Compare the eighth family, where the struct was a unit struct and its constructor claimed the value namespace - the difference between the two programs is entirely whether the struct brings a constructor with it. Do not rename either item here; the code is legal as written, if denser than most readers would choose. - A type alias and a constant of the same name compile for the same reason. The alias claims X in the type namespace and the constant claims X in the value namespace:
Rust
type X = i32;
const X: i32 = 7;
fn main() { let v: X = X; println!("{}", v); }
Terminal
$ rustc --edition 2021 trap_alias_const.rs -o trap_alias_const
$ ./trap_alias_const
7

The output is 7. The one line let v: X = X; uses both claims - the X before the = is the type in the annotation, the X after it is the constant in the expression - and compiles precisely because those positions consult different namespaces. This is the Reference's namespaces example in miniature: a name in one namespace does not conflict with the same name in another. - A tuple struct and a macro of the same name compile - the macro namespace is a third, separate pool. The tuple struct claims Foo in the type namespace and its constructor in the value namespace, and macro_rules! Foo claims Foo only in the macro namespace:

Rust
struct Foo(u32);
macro_rules! Foo { () => { 9 }; }
fn main() { let f = Foo(3); println!("{} {}", f.0, Foo!()); }
Terminal
$ rustc --edition 2021 trap_tuple_macro.rs -o trap_tuple_macro
$ ./trap_tuple_macro
3 9

The output is 3 9: Foo(3) is the constructor in the value namespace, and Foo!() is the macro, and the ! in the syntax is what routes the second use to the macro namespace. This is the shape the Reference's namespaces chapter itself uses to show overlapping names in different namespaces being used unambiguously. - The same type name in two sibling modules compiles - the claims land in different scopes. Each module is its own scope for name claims, so Item inside a and Item inside b are two names that happen to be spelled alike, reached apart by their paths:

Rust
mod a { pub struct Item { pub v: i32 } }
mod b { pub struct Item { pub v: i32 } }
fn main() { let x = a::Item { v: 1 }; let y = b::Item { v: 2 }; println!("{} {}", x.v, y.v); }
Terminal
$ rustc --edition 2021 trap_nested_mod.rs -o trap_nested_mod
$ ./trap_nested_mod
1 2

The output is 1 2. Compare the fifth family: two modules named util in the one parent collide, while two types named Item in two different parents do not. Scope is the second axis of the rule, and moving a duplicate definition into a submodule - the move form of the fix - works by putting clear scope between the two claims. - The same item name defined again inside an inner block compiles, for the same per-scope reason. A block is a scope, and an item declared inside it claims its name there, shadowing the outer item for the block's extent:

Rust
struct Item { v: i32 }
fn main() { let a = Item { v: 1 }; { struct Item { w: i32 } let b = Item { w: 2 }; println!("{}", b.w); } println!("{}", a.v); }
Terminal
$ rustc --edition 2021 trap_inner_scope.rs -o trap_inner_scope
$ ./trap_inner_scope
2
1

The output is 2 then 1: inside the block Item is the inner struct with its w field, and after the block Item is the outer struct again. Items declared in a block are local to it in the same way the module items in the previous trap are local to their module - a duplicate that is really a scoped namesake is not a duplicate at all. - A second let of the same name compiles - a binding shadows, it does not redefine. Local bindings are not item definitions: a second let x creates a new binding that hides the first from that point on, which is the ordinary shadowing every Rust program uses to transform a value step by step:

Rust
fn main() { let x = 1; let x = x + 1; println!("{}", x); }
Terminal
$ rustc --edition 2021 trap_shadow.rs -o trap_shadow
$ ./trap_shadow
2

The output is 2. The contrast is the whole trap: let bindings live by shadowing, items live by unique claim. Two functions, structs, or constants of one name are E0428 because items have no shadowing rule - the second definition does not hide the first, it collides with it. Code that expects a later item definition to override an earlier one, as a later let overrides, is importing the wrong model. - Importing the same name twice is E0252, not E0428. Neither Baz is defined twice in this file - each is defined once, in its own module - but two use declarations try to bring the name Baz into this module, and imports cannot create duplicate bindings of one name in a module:

Rust
mod foo { pub struct Baz; }
mod bar { pub struct Baz; }
use foo::Baz;
use bar::Baz;
fn main() { let _x = Baz; }
Terminal
$ rustc --edition 2021 trap_e0252.rs -o trap_e0252
error[E0252]: the name `Baz` is defined multiple times
 --> trap_e0252.rs:4:5
  |
3 | use foo::Baz;
  |     -------- previous import of the type `Baz` here
4 | 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
  |
4 | use bar::Baz as OtherBaz;
  |              +++++++++++

warning: unused import: `bar::Baz`
 --> trap_e0252.rs:4:5
  |
4 | use bar::Baz;
  |     ^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` (part of `#[warn(unused)]`) on by default

error: aborting due to 1 previous error; 1 warning emitted

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

The header text is nearly this guide's - "the name Baz is defined multiple times" - and the note names the same namespace rule, which is why the two codes are confused. The tells are the code itself, the word "reimported" in the second span where E0428 says "redefined", and the first span's "previous import" where E0428 names a definition. The error codes index defines E0252 as two items of the same name that cannot be imported without rebinding one of them under a new local name, and its repair is the one the help prints: alias one import with as - use bar::Baz as OtherBaz; - leaving both definitions untouched. Defining twice is E0428 and is fixed at a definition; importing twice is E0252 and is fixed at an import. (The unused-import warning rides along because the refused import binds nothing; it disappears with the alias.) - A duplicate method inside one impl is E0592, not E0428. The two go methods are not two items in a module namespace - they are two entries in one implementation's method list, and that collision has its own code and its own header:

Rust
struct S;
impl S { fn go(&self) -> i32 { 1 } fn go(&self) -> i32 { 2 } }
fn main() { println!("{}", S.go()); }
Terminal
$ rustc --edition 2021 trap_e0592.rs -o trap_e0592
error[E0592]: duplicate definitions with name `go`
 --> trap_e0592.rs:2:36
  |
2 | impl S { fn go(&self) -> i32 { 1 } fn go(&self) -> i32 { 2 } }
  |          -------------------       ^^^^^^^^^^^^^^^^^^^ duplicate definitions for `go`
  |          |
  |          other definition for `go`

error: aborting due to 1 previous error

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

The header reads "duplicate definitions with name go" - no "defined multiple times", no namespace note. The repair is the familiar one in miniature: rename or delete one method inside the impl. But the code matters for the search: a programmer who meets a duplicate method and searches this guide's error will not find their program here, because the module-namespace rule that produces E0428 is not the rule an impl's method list is checked by. - A second implementation of the same trait for the same type is E0119, not E0428. Nothing in this program is named twice - the trait is defined once and the type is defined once - what is duplicated is the pairing of the two in an impl, and the compiler reports a conflict between implementations:

Rust
trait Greet { fn hi(&self) -> i32; }
struct S;
impl Greet for S { fn hi(&self) -> i32 { 1 } }
impl Greet for S { fn hi(&self) -> i32 { 2 } }
fn main() { println!("{}", S.hi()); }
Terminal
$ rustc --edition 2021 trap_e0119.rs -o trap_e0119
error[E0119]: conflicting implementations of trait `Greet` for type `S`
 --> trap_e0119.rs:4:1
  |
3 | impl Greet for S { fn hi(&self) -> i32 { 1 } }
  | ---------------- first implementation here
4 | impl Greet for S { fn hi(&self) -> i32 { 2 } }
  | ^^^^^^^^^^^^^^^^ conflicting implementation for `S`

error: aborting due to 1 previous error

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

The header reads "conflicting implementations of trait Greet for type S". Compare the seventh family: two traits of one name are E0428, because names collide; one trait implemented twice for one type is E0119, because implementations collide. Renaming fixes the first and does nothing for the second - one of the two impl blocks must go, or be changed to a different trait or a different type. - A duplicate field in a struct is E0124, not E0428. Fields are not in any namespace - the Reference notes that even though struct fields are named, the named fields do not live in an explicit namespace, and are reached only through a field expression on a value - so a repeated field is checked as a defect in the struct's own declaration list, under its own code:

Rust
struct S { x: i32, x: i32 }
fn main() { let _s = S { x: 1 }; }
Terminal
$ rustc --edition 2021 trap_field_dup.rs -o trap_field_dup
error[E0124]: field `x` is already declared
 --> trap_field_dup.rs:1:20
  |
1 | struct S { x: i32, x: i32 }
  |            ------  ^^^^^^ field already declared
  |            |
  |            `x` first declared here

error: aborting due to 1 previous error

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

The header reads "field x is already declared". Three different lists in one program can each hold a duplicate - the module's items, an impl's methods, a struct's fields - and each list reports under its own code: E0428, E0592, E0124. Identifying which list the duplicate sits in is the first step of every fix in this area, and the error code is what identifies it.

Decision summary

  • The header says "the name X is defined multiple times", the code is E0428, and the note names the value namespace of the module, with two functions, two constants, a constant and a static, or a function and a constant as the pair: rename or delete one - kind does not separate names inside a namespace.
  • The header and code are the same and the note names the type namespace of the module, with two structs, a struct and an enum, two aliases, a struct and an alias, two modules, or two traits as the pair (E0428): rename one, or move one into a submodule.
  • The header and code are the same, the note names the value namespace, and one of the pair is a struct (E0428): the struct's unit or tuple constructor is the claimant - rename the other value item, or give the struct named fields.
  • The header and code are the same and the note names the type namespace "of this enum" (E0428): two variants of one enum share a name - rename the second variant.
  • The same name appears as a type and as a constant, a type alias and a constant, or a tuple struct and a macro, and the program compiles: no error - the claims are in different namespaces, and each use is read in its position's namespace.
  • The same type name is defined in two sibling modules, or again in an inner block, and the program compiles: no error - the claims are in different scopes.
  • A name is bound twice by let and the program compiles: no error - bindings shadow; only items collide.
  • The header says "the name X is defined multiple times" but the code is E0252 and the second span says "reimported here": the name was imported twice, not defined twice - alias one import with as, the subject of the neighboring E0252 guide.
  • The header says "duplicate definitions with name" (E0592): two methods of one name inside one impl - rename or delete one method.
  • The header says "conflicting implementations of trait" (E0119): one trait implemented twice for one type - remove or change one impl; no rename is involved.
  • The header says "field x is already declared" (E0124): two fields of one name in one struct - rename or delete one field.

Checklist

  • The colliding name read off the header, and both definitions located from the two spans - the "previous definition" and the "redefined here" - before anything is renamed.
  • The namespace read off the note: value, type, or the type namespace of an enum - the namespace, not the items' kinds, is what makes the pair a collision.
  • Decided whether the duplicate is an accident or two real things: a paste or merge leftover is deleted; two real things are separated by a rename or a move into a submodule.
  • Every use of the renamed definition renamed with it - constructions, annotations, paths, trait bounds, patterns, and imports of it from other modules.
  • Struct constructors accounted for: a unit or tuple struct spends its name in the value namespace as well as the type namespace, so constants, statics, and functions were checked against the constructor, not only against the type.
  • Cross-namespace namesakes left alone: a type and a constant, an alias and a constant, or a struct and a macro sharing a name is legal code, and renaming it is churn.
  • Imports checked separately from definitions: a name that arrives twice through use is E0252 and takes an as alias, not a rename of anything defined in the file.
  • After the rename or move, the program compiled again and run: the fix is finished only when both definitions are reachable under their own names and the output is what the program was written to produce.

Sources

  • Rust, Error codes index: E0428 - the definition of the error: a type or module has been defined more than once - with the two-Bar struct example, and the prescription to verify you did not misspell the type or module's name, or remove or rename the duplicated one.
  • The Rust Reference, Namespaces - a namespace is a logical grouping of declared names, segregated by the kind of entity the name refers to, so that a name in one namespace does not conflict with the same name in another; the lists of what each namespace holds - the type namespace holding module declarations, structs, enums and their variants, traits, and type aliases, the value namespace holding functions, constants, statics, and struct and enum variant constructors, the macro namespace holding macro_rules declarations - the example of a tuple struct and a macro sharing the name Foo, and the note that struct fields, though named, do not live in an explicit namespace and that a use declaration can introduce aliases into multiple namespaces.
  • The Rust Reference, Names - an entity is a construct that can be referred to, usually via a path; a declaration introduces a name to refer to an entity, valid within a scope; names are segregated into different namespaces, allowing entities in different namespaces to share the same name without conflict; and name resolution is the compile-time process of tying paths and identifiers to declarations.
  • Rust, Error codes index: E0252 - the neighboring import error: two items of the same name cannot be imported without rebinding one of the items under a new local name - with the as alias as its fix, the code a name imported twice produces, where defining the name twice is E0428.

Keep Exploring