E0026 Struct Variant Has No Field Fix

Written December 2, 2026 · Tested with Rust 1.99.1 · 12 min read

By the end of this guide
  • The rule underneath: pattern fields come from the definition
  • How to read the diagnostic
  • The first failure: the index example
  • The same error in long form, and on a variant

Written December 2, 2026

Tested with Rust 1.99.1

What this guide is

Error E0026 means a pattern named a field that does not exist. The error codes index puts it in one sentence: "A struct pattern attempted to extract a nonexistent field from a struct."

In the shapes probed in this guide, E0026 fires on the pattern side: a match pattern for a plain struct, or for a struct-like enum variant, names a field the definition does not have. The same wrong name on the construction side draws a different code - E0560 for a plain struct literal and E0559 for a variant literal. This guide shows the E0026 shapes, the neighboring construction-side shapes, and the repairs.

Build every program in this guide with rustc --edition 2021. In the compiler transcripts, the file names are the bare names shown on the command line.

The rule underneath: pattern fields come from the definition

A struct pattern can only extract fields the type actually declares. The names before the colon in a pattern are field names; the names after the colon are the new bindings. The index page makes the same point: struct fields are identified by the name used before the colon, so struct patterns should resemble the declaration of the struct type being matched.

That split is where this error usually starts. A shorthand pattern such as z asks for a field named z and binds it to a variable also named z. If you only wanted a local variable named z holding an existing field, the field name still has to come first: y: z extracts field y and binds it as z.

How to read the diagnostic

The E0026 header names both sides: struct Thing does not have a field named z, or variant Shape::Rect does not have a field named depth. Read the field name first - that is the name to fix, rename, or add to the definition.

The span is on the bad field name in the pattern, with the note that the struct or variant does not have this field. When a similar field exists, the help proposes it: in the plain-struct shapes probed, the help rewrites the bad name to y. In the variant pattern shape, the help is attached to the span and reads that Shape::Rect has a field named h.

Watch for a second error riding along. A pattern that names a nonexistent field usually also leaves a real field unmentioned, and that draws E0027 on its own line. Fixing the name often clears both; adding a field to a definition does not, unless the pattern also accounts for the fields it does not extract - see the repairs.

The first failure: the index example

The index's erroneous example, verbatim:

Rust
#![allow(unused)]
fn main() {
struct Thing {
    x: u32,
    y: u32,
}
let thing = Thing { x: 0, y: 0 };
match thing {
    Thing { x, z } => {} // error: `Thing::z` field doesn't exist
}
}
Output
error[E0026]: struct `Thing` does not have a field named `z`
 --> fail_index.rs:9:16
  |
9 |     Thing { x, z } => {} // error: `Thing::z` field doesn't exist
  |                ^ struct `Thing` does not have this field
  |
help: a field with a similar name exists
  |
9 -     Thing { x, z } => {} // error: `Thing::z` field doesn't exist
9 +     Thing { x, y } => {} // error: `Thing::z` field doesn't exist
  |

error: aborting due to 1 previous error

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

Exactly one E0026, and nothing else. The literal Thing { x: 0, y: 0 } on the construction side is correct and draws nothing; only the pattern's z fails.

The same error in long form, and on a variant

The long form fails the same way. Writing the bindings out in full - Thing { x: a, z: b } - still puts the field name z before the colon, so it draws exactly one E0026, with the same similar-name help rewriting z: b to y: b:

Rust
fn main() {
    struct Thing { x: u32, y: u32 }
    let thing = Thing { x: 1, y: 2 };
    match thing {
        Thing { x: a, z: b } => { println!("{} {}", a, b); }
    }
}
Output
error[E0026]: struct `Thing` does not have a field named `z`
 --> fail_pattern_long.rs:5:23
  |
5 |         Thing { x: a, z: b } => { println!("{} {}", a, b); }
  |                       ^ struct `Thing` does not have this field

A struct-like enum variant pattern behaves the same, with one addition. Naming depth on Shape::Rect, which declares only w and h, draws one E0026 - and because the pattern then never mentions h, it also draws one E0027:

Rust
fn main() {
    enum Shape { Rect { w: u32, h: u32 }, Circle { r: u32 } }
    let s = Shape::Rect { w: 3, h: 4 };
    match s {
        Shape::Rect { w, depth } => { println!("{} {}", w, depth); }
        _ => {}
    }
}
Output
error[E0026]: variant `Shape::Rect` does not have a field named `depth`
 --> fail_enum_pattern_wrong.rs:5:26
  |
5 |         Shape::Rect { w, depth } => { println!("{} {}", w, depth); }
  |                          ^^^^^
  |                          |
  |                          variant `Shape::Rect` does not have this field
  |                          help: `Shape::Rect` has a field named `h`

error[E0027]: pattern does not mention field `h`
 --> fail_enum_pattern_wrong.rs:5:9
  |
5 |         Shape::Rect { w, depth } => { println!("{} {}", w, depth); }
  |         ^^^^^^^^^^^^^^^^^^^^^^^^ missing field `h`

So E0026 is not limited to plain structs: in the probed shape it fires for a struct-like variant pattern too, with the header wording switched from "struct" to "variant."

The shapes that are not this error

The construction side has its own codes. A plain struct literal naming z draws E0560, not E0026, with the span wording "unknown field" and the same similar-name help:

Rust
fn main() {
    struct Thing { x: u32, y: u32 }
    let thing = Thing { x: 0, z: 0 };
    println!("{}", thing.x);
}
Output
error[E0560]: struct `Thing` has no field named `z`
 --> fail_struct_literal.rs:3:31
  |
3 |     let thing = Thing { x: 0, z: 0 };
  |                               ^ unknown field

A struct-like variant literal naming a field the variant does not have draws E0559 - whether the bad name replaces a real field or is added after all the real ones. In the extra-field shape the note reads "all struct fields are already assigned":

Rust
fn main() {
    enum Shape { Rect { w: u32, h: u32 }, Circle { r: u32 } }
    let s = Shape::Rect { w: 3, h: 4, depth: 5 };
    match s { _ => {} }
}
Output
error[E0559]: variant `Shape::Rect` has no field named `depth`
 --> fail_enum_construct_extra.rs:3:39
  |
3 |     let s = Shape::Rect { w: 3, h: 4, depth: 5 };
  |                                       ^^^^^ `Shape::Rect` does not have this field
  |
  = note: all struct fields are already assigned

Writing a tuple variant with named fields is also E0559, once per bad name - two errors for x and y - and the help switches to tuple syntax:

Rust
fn main() {
    enum Pair { Both(u32, u32) }
    let p = Pair::Both { x: 1, y: 2 };
    match p { _ => {} }
}
Output
error[E0559]: variant `Pair::Both` has no field named `x`
 --> fail_tuple_named.rs:3:26
  |
2 |     enum Pair { Both(u32, u32) }
  |                 ---- `Pair::Both` defined here
3 |     let p = Pair::Both { x: 1, y: 2 };
  |                          ^ field does not exist
  |
help: `Pair::Both` is a tuple variant, use the appropriate syntax
  |
3 -     let p = Pair::Both { x: 1, y: 2 };
3 +     let p = Pair::Both(/* u32 */, /* u32 */);

A missing field is a different mistake from a wrong one. A struct literal that omits y entirely draws E0063, "missing field y in initializer of Thing" - no bad name is involved.

And a tuple struct matched with a struct-style pattern draws E0769, "tuple variant Pair written as struct variant," with a help that rewrites the pattern in tuple form, Pair(x, y). The pattern syntax has to match the shape of the definition, not just its field names.

Field-name errors split by side. For the construction-side code on a struct-like variant, see the E0559 guide when it is published. For a pattern that leaves a real field unmentioned, the neighboring code is E0027. For a struct literal that omits a field, the neighboring code is E0063.

The repairs

Every repair below makes the pattern and the definition agree: either the pattern names a field that exists, or the definition gains the field the pattern needs.

Name the correct field. When the definition is right, follow the help and use the real name; the corrected pattern compiled and ran in the probed shape, printing 1 2:

Rust
fn main() {
    struct Thing { x: u32, y: u32 }
    let thing = Thing { x: 1, y: 2 };
    match thing {
        Thing { x, y } => { println!("{} {}", x, y); }
    }
}
Output
1 2

Rename in the pattern when you want a different local name. This is the index page's own repair: y: z extracts field y and binds it as z. It compiled and ran in the probed shape, printing 0:

Rust
#![allow(unused)]
fn main() {
struct Thing {
    x: u32,
    y: u32,
}
let thing = Thing { x: 0, y: 0 };
match thing {
    Thing { x, y: z } => { println!("{}", z); }
}
}
Output
0

Add the field to the definition when the pattern expresses what the code needs. Adding z to Thing makes the name legal - but the pattern must still account for y, so this repair pairs the new field with .. for the rest. Without .., the first version of this repair drew E0027 for the unmentioned y; with it, the program compiled and ran, printing 1 3:

Rust
fn main() {
    struct Thing { x: u32, y: u32, z: u32 }
    let thing = Thing { x: 1, y: 2, z: 3 };
    match thing {
        Thing { x, z, .. } => { println!("{} {}", x, z); }
    }
}
Output
1 3

Ignore the remaining fields with .. when you only need some of them. The rest pattern lets a pattern name a subset of fields without mentioning the others, and it compiled and ran in the probed shape, printing 1:

Rust
fn main() {
    struct Thing { x: u32, y: u32 }
    let thing = Thing { x: 1, y: 2 };
    match thing {
        Thing { x, .. } => { println!("{}", x); }
    }
}
Output
1

Match a variant with its real fields. The variant pattern repair uses the fields Rect actually declares, and it compiled and ran in the probed shape, printing 3 4. Renaming works there too - h: height binds field h as height and also printed 3 4 in the probed shape:

Rust
fn main() {
    enum Shape { Rect { w: u32, h: u32 }, Circle { r: u32 } }
    let s = Shape::Rect { w: 3, h: 4 };
    match s {
        Shape::Rect { w, h } => { println!("{} {}", w, h); }
        _ => {}
    }
}
Output
3 4

Fixes in reach order

First, check which side of the colon the bad name sits on. In the probed shapes, a name before the colon - or a shorthand name, which is both field and binding - must be a field the definition declares. A name after the colon is a free choice of local variable name and never draws this error.

Second, use the header to tell pattern from construction. "does not have a field named" in a pattern is E0026; the near-identical construction wordings are E0560 for a struct literal ("has no field named," span marked "unknown field") and E0559 for a variant literal ("has no field named"). The fix direction is the same, but the code tells you which expression to edit.

Third, decide which side is wrong. If the definition is right, rename the pattern field to a real one, or use field: local to keep your local name. If the pattern is right, add the field to the struct or variant definition - and check the literal that builds the value too, since it must now supply the new field.

Fourth, clear any E0027 that rode along. In the probed shapes it appeared when the bad name left a real field unmentioned, and when a field was added to a definition without updating the pattern. Naming the missing field, binding it with field: _, or adding .. to the pattern each account for it.

Last, leave tuple shapes in tuple syntax. In the probed shapes, named fields on a tuple variant drew E0559 on construction and a struct-style pattern on a tuple struct drew E0769; neither is fixed by renaming a field.

Decision summary

E0026 fired in the probed shapes for a plain struct pattern naming a nonexistent field - the index example (exactly one E0026) and the same pattern in long form (exactly one E0026) - and for a struct-like enum variant pattern naming a nonexistent field (exactly one E0026, plus one E0027 for the real field the pattern left unmentioned). It did not fire on the construction side: a struct literal with the same wrong name drew exactly one E0560, a struct-like variant literal with a wrong or extra field name drew exactly one E0559 each, and a tuple variant written with two named fields drew exactly two E0559. It also did not fire for a struct literal missing a field (exactly one E0063) or a tuple struct matched with a struct-style pattern (exactly one E0769). The repairs that compiled and ran were naming the correct field (printing 1 2), renaming a field in the pattern as y: z (printing 0), adding the field to the definition with .. in the pattern for the remaining field (printing 1 3), ignoring the remaining fields with .. (printing 1), and matching a struct-like variant with its real fields, plain or renamed (printing 3 4 in both shapes).

Checklist

  • E0026 is pattern-side in the probed shapes: a struct or struct-like variant pattern names a field the definition does not have.
  • The name before the colon is the field; the name after it is your local binding - y: z is the rename form, and bare z asks for a field named z.
  • The same wrong name in a literal is E0560 for a plain struct and E0559 for a variant, in the probed shapes - check the code before searching for a fix.
  • A bad pattern name often brings E0027 with it, for the real field the pattern then leaves unmentioned; account for it by name, with field: _, or with ...
  • Adding a field to a definition is a valid repair only when the pattern and the constructing literal are updated to match, in the probed shapes.
  • Tuple variants and tuple structs take tuple syntax on both sides; named-field syntax on them drew E0559 and E0769 in the probed shapes, not E0026.

Sources

  • Rust error codes index, E0026 - a struct pattern attempted to extract a nonexistent field from a struct; the erroneous example matches on a struct with fields x and y using a pattern that names z, and the page explains that fields are identified by the name before the colon and shows renaming y to z as y: z.
  • Rust error codes index, E0559 - the code drawn in the probed shapes by a struct-like variant literal naming a field the variant does not have, and by a tuple variant written with named fields.
  • Rust error codes index, E0560 - the code drawn in the probed shape by a plain struct literal naming a field the struct does not have.

Keep Exploring