Rust
E0124 Field Already Declared Fix
E0124 is a declaration error, not a use error: the struct definition itself names one field twice, so no value of the type can ever be built until the declaration is fixed. The compiler points at both declarations - the first one and the repeat - which makes the usual cause easy to spot: the second field was almost always meant to carry a different name, often a field the struct is missing everywhere it is constructed. This guide works the shapes that produce E0124, shows the repairs running, and separates the neighbors that look similar but carry different codes: E0062 when a field is set twice in a struct literal, E0592 for a duplicate method, and E0428 for a duplicate type name.
Written November 30, 2026 · Tested with Rust 1.99.0 · 14 min read
- The rule underneath: one name, one field, per declaration
- How to read the diagnostic
- The first failures: the duplicate in a named struct
- What is allowed: the controls
Written November 30, 2026
Tested with Rust 1.99.0
What this guide is
Error E0124 means a struct was declared with two fields having the same name. The error codes index states it in one line - "A struct was declared with two fields having the same name" - and its advice is to "verify that the field names have been correctly spelled", showing the repair as renaming the second field so the two fields carry different names.
The practical payoff is deciding which of the two declarations is wrong. If the second field was meant to be a different field - the common case, a copy-paste or a misspelled name - rename it, and expect the struct's constructors to need that new field supplied, which is the territory of the E0063 missing field guide. If the second declaration is simply an accidental repeat of the first, delete it. Both repairs are shown running below.
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 name, one field, per declaration
A struct is a nominal type whose named fields are identified by name, per the Rust Reference on structs. In the probed set, the name is the identity: two fields with the same name collide even when everything else about them differs - a different type, a different visibility, a generic parameter type, or other fields in between. Nothing about the second declaration can distinguish it from the first, because code that uses the struct refers to the field by that one name.
The rule is about declarations, not positions or adjacency. In the probed set it fired for a duplicate separated by another field, for a struct declared inside a function body, and for the struct-like field set of an enum variant and of a union. It did not fire for a tuple struct, whose fields have no names to collide. Where the duplicate happens somewhere other than a declaration - a value being built, a method being defined, a type being named - a different code applies, as the neighbors below show.
How to read the diagnostic
Every E0124 failure in the probed set carries the same header naming the field: "field field1 is already declared". Two spans appear: the repeat is underlined and labeled "field already declared", and the first declaration is underlined and labeled "field1 first declared here". Read those two spans as the whole decision: one of those two lines is the field you meant, and the other is either a rename or a deletion. With three occurrences of one name, the compiler reported each repeat separately - two E0124 errors in the probed case, each pointing back to the same first declaration - so fix the name once and every repeat is addressed, rather than treating each error as a separate problem.
The first failures: the duplicate in a named struct
The smallest shape is the error index's own example - the same field name, the same type, twice in one declaration:
struct Foo {
field1: i32,
field1: i32,
}
fn main() {
let _f = Foo { field1: 1 };
}
error[E0124]: field `field1` is already declared
--> p1.rs:3:5
|
2 | field1: i32,
| ----------- `field1` first declared here
3 | field1: i32,
| ^^^^^^^^^^^ field already declared
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0124`.
Changing the second field's type does not help. The collision is on the name, so a duplicate with a different type fails identically:
struct Foo {
value: i32,
value: String,
}
fn main() {}
error[E0124]: field `value` is already declared
--> p2.rs:3:5
|
2 | value: i32,
| ---------- `value` first declared here
3 | value: String,
| ^^^^^^^^^^^^^ field already declared
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0124`.
Visibility does not distinguish the fields either. A pub first field and a private second field with the same name are still one name declared twice, in the probed set:
struct Foo {
pub field1: i32,
field1: i32,
}
fn main() {}
error[E0124]: field `field1` is already declared
--> p6.rs:3:5
|
2 | pub field1: i32,
| --------------- `field1` first declared here
3 | field1: i32,
| ^^^^^^^^^^^ field already declared
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0124`.
Nor does distance. A duplicate with another field in between fails the same way, with the spans reaching across the intervening field:
struct Foo {
count: u32,
label: String,
count: u32,
}
fn main() {}
error[E0124]: field `count` is already declared
--> p12.rs:4:5
|
2 | count: u32,
| ---------- `count` first declared here
3 | label: String,
4 | count: u32,
| ^^^^^^^^^^ field already declared
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0124`.
The declaration's location does not matter. A struct declared inside a function body, and a generic struct whose duplicate field uses the type parameter, both fail with E0124 in the probed set:
fn main() {
struct Local {
x: i32,
x: i32,
}
let _l = Local { x: 1 };
}
error[E0124]: field `x` is already declared
--> p8.rs:4:9
|
3 | x: i32,
| ------ `x` first declared here
4 | x: i32,
| ^^^^^^ field already declared
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0124`.
struct Wrap<T> {
item: T,
item: T,
}
fn main() {}
error[E0124]: field `item` is already declared
--> p9.rs:3:5
|
2 | item: T,
| ------- `item` first declared here
3 | item: T,
| ^^^^^^^ field already declared
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0124`.
Three occurrences of one name produce one error per repeat - two errors here, both anchored on the first declaration:
struct Foo {
a: i32,
a: i32,
a: i32,
}
fn main() {}
error[E0124]: field `a` is already declared
--> p7.rs:3:5
|
2 | a: i32,
| ------ `a` first declared here
3 | a: i32,
| ^^^^^^ field already declared
error[E0124]: field `a` is already declared
--> p7.rs:4:5
|
2 | a: i32,
| ------ `a` first declared here
3 | a: i32,
4 | a: i32,
| ^^^^^^ field already declared
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0124`.
What is allowed: the controls
A tuple struct is the control that shows the rule is about names. Its fields are positional and unnamed, so two fields of the same type declare nothing twice, and the program compiles and runs:
struct Pair(i32, i32);
fn main() {
let p = Pair(1, 2);
println!("{}", p.0 + p.1);
}
3
The output is 3, the sum of the two positional fields. There is no E0124 repair to apply to a tuple struct - if a duplicate-name error appears after converting a tuple struct to a named struct, the fix is in the new names, not in the field count.
The baseline named struct - the error index's repaired example, two fields with two different names - also compiles and runs:
struct Foo {
field1: i32,
field2: i32,
}
fn main() {
let f = Foo { field1: 1, field2: 2 };
println!("{}", f.field1 + f.field2);
}
3
The output is 3, the sum of the two distinctly named fields. That is the shape every repair below returns to: one name per field, each name used consistently at construction.
The traps: neighbors that are not E0124
First, the same field set twice in a struct literal - building a value, not declaring a type - is E0062, "field specified more than once", not E0124. The declaration here is fine; the expression repeats a field:
struct Foo {
field1: i32,
field2: i32,
}
fn main() {
let _f = Foo { field1: 1, field1: 2, field2: 3 };
}
error[E0062]: field `field1` specified more than once
--> p10.rs:6:31
|
6 | let _f = Foo { field1: 1, field1: 2, field2: 3 };
| --------- ^^^^^^ used more than once
| |
| first use of `field1`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0062`.
The split to remember: E0124 points into the type declaration and labels a field "already declared"; E0062 points into the expression and labels a field "used more than once". The E0062 repair is in the literal - remove the repeated assignment - and needs no change to the struct.
Second, a duplicate method name is E0592, covered in full in the E0592 duplicate method guide. Methods are definitions in an impl, not fields in a declaration:
struct Foo;
impl Foo {
fn go(&self) {}
fn go(&self) {}
}
fn main() {
let f = Foo;
f.go();
}
error[E0592]: duplicate definitions with name `go`
--> n1.rs:4:5
|
3 | fn go(&self) {}
| ------------ other definition for `go`
4 | fn go(&self) {}
| ^^^^^^^^^^^^ duplicate definitions for `go`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0592`.
Third, declaring the type itself twice is E0428, covered in full in the E0428 duplicate definition guide. Here no field repeats - the name Foo does:
struct Foo {
a: i32,
}
struct Foo {
b: i32,
}
fn main() {}
error[E0428]: the name `Foo` is defined multiple times
--> n2.rs:4:1
|
1 | struct Foo {
| ---------- previous definition of the type `Foo` here
...
4 | struct Foo {
| ^^^^^^^^^^ `Foo` redefined here
|
= note: `Foo` 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`.
Last, two declaration shapes do share E0124 in the probed set. The struct-like field set of an enum variant is a field declaration, and a duplicate there is E0124:
enum Shape {
Rect { width: u32, width: u32 },
}
fn main() {}
error[E0124]: field `width` is already declared
--> p4.rs:2:24
|
2 | Rect { width: u32, width: u32 },
| ---------- ^^^^^^^^^^ field already declared
| |
| `width` first declared here
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0124`.
A union's fields are likewise named declarations, and a duplicate there is also E0124 in the probed set:
union U {
a: u32,
a: u32,
}
fn main() {}
error[E0124]: field `a` is already declared
--> p11.rs:3:5
|
2 | a: u32,
| ------ `a` first declared here
3 | a: u32,
| ^^^^^^ field already declared
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0124`.
So the code follows the kind of declaration, not the keyword that introduces it: named field declarations collide as E0124 wherever they appear in the probed set, while duplicates in expressions, method definitions, and type names carry their own codes.
Fixes in reach order
First, rename the field that was meant to have its own name. This is the error index's own repair, and the usual case: the second declaration was a copy of the first, or a misspelling of the intended name. The p5 shape - a user struct whose second field repeats name where a distinct field was intended - is the pattern:
struct User {
name: String,
name: String,
email: String,
}
fn main() {}
error[E0124]: field `name` is already declared
--> p5.rs:3:5
|
2 | name: String,
| ------------ `name` first declared here
3 | name: String,
| ^^^^^^^^^^^^ field already declared
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0124`.
Give the second field its intended name and supply it at construction, and the program compiles and runs:
struct User {
name: String,
username: String,
email: String,
}
fn main() {
let u = User {
name: String::from("Ada"),
username: String::from("ada1"),
email: String::from("a@example.com"),
};
println!("{} {} {}", u.name, u.username, u.email);
}
Ada ada1 a@example.com
The output is Ada ada1 a@example.com - the person's name, the now-distinct username field, and the email, each read back from its own field. After a rename, expect construction sites to report any field they do not yet supply; that follow-on is E0063 territory, handled in the E0063 guide.
Second, when the duplicate is a pure accident - one field, declared twice, with nothing distinct intended - delete the repeat. The p1 failure with the second declaration removed compiles and runs:
struct Foo {
field1: i32,
}
fn main() {
let f = Foo { field1: 7 };
println!("{}", f.field1);
}
7
The output is 7, the single field's value read back. Deletion is the right repair only when the struct genuinely has one field of that kind; if any code expected two values, the rename above is the repair and deletion would move the error to the use sites.
Third, the minimal rename: where the two declarations were always meant to be two fields, renaming the second one - the error index's field1/field2 form - is the whole fix:
struct Foo {
field1: i32,
field2: i32,
}
fn main() {
let f = Foo { field1: 1, field2: 2 };
println!("{}", f.field1 + f.field2);
}
3
The output is 3, the sum of the two fields. No other change is needed in this program, because its construction already supplies both fields: the E0124 error itself never leaves a follow-on once the names are distinct, but a construction site that does not yet supply the renamed field is a separate E0063, as the first fix notes.
Last, when the neighboring code is the real one, switch playbooks: E0062 means fix the struct literal, not the declaration; E0592 means rename or remove one of the duplicate methods, per the E0592 guide; and E0428 means rename or remove one of the duplicate type definitions, per the E0428 guide.
Decision summary
E0124, "field is already declared", is a duplicate field name in a declaration, in this guide's probed set. It fired for a named struct with the same name twice - with the same type, with different types, with different visibility, with another field in between, inside a function body, and on a generic struct - and for three occurrences of one name it fired once per repeat. It also fired for the struct-like fields of an enum variant and for a union. It did not fire for a tuple struct, whose fields are unnamed, or for a named struct whose fields all carry distinct names. The neighbors are distinct in the probed set: a field set twice in a struct literal is E0062, a duplicate method definition is E0592, and a duplicate type name is E0428. The repairs act on the declaration: rename the field that was meant to be distinct, or delete the accidental repeat.
Checklist
- Read both spans before editing: in the probed set, the diagnostic marks the first declaration and the repeat, and one of those two lines is the change.
- Decide rename versus delete first: in the probed set, a misspelled or copied name is a rename, and a pure accidental repeat is a deletion - the wrong choice moves the error to construction or use sites.
- Do not try to distinguish the duplicate by type or visibility: in the probed set, neither a different type nor a
pubmarker separated two fields with the same name. - Count the errors against the occurrences: in the probed set, three declarations of one name produced two E0124 errors, all anchored on the first declaration.
- Check where the duplicate lives before applying this guide: a repeat in a struct literal is E0062, in an
implis E0592, and in a type name is E0428 - each with its own repair. - After a rename, supply the new field everywhere the struct is built: a missing field at construction is E0063, not a return of E0124.
Related guides on this site
- E0128 Forward Declared Identifier in Default Fix
- E0026 Struct Variant Has No Field Fix
- Field Specified More Than Once E0062 Fix
- Tuple Pattern Field Count E0023 Fix
Sources
- Rust error codes index, E0124 - a struct was declared with two fields having the same name; the erroneous example declaring
field1twice instruct Foo; the advice to verify that the field names have been correctly spelled; the repaired example renaming the second field tofield2. - The Rust Reference, Structs - a struct is a nominal type defined with the
structkeyword, and a struct declaration defines the given name in the type namespace of the module or block where it is located; a tuple struct is a nominal tuple type that also defines a constructor of the same name in the value namespace; a unit-like struct is a struct without any fields. - Rust error codes index, E0062 - the neighboring expression error: a field was specified more than once in a struct literal.
Keep Exploring
The full Rust hub \u2014 ownership, structs and enums, async, and measured comparisons.
Every guide on the site in one list, grouped by topic.
Every browser-only checker and verifier on the site.
The C++, Python, Sysadmin, Rust, Go, and TypeScript hubs.