Skip to content

Rust Error E0026: Struct or Variant Does Not Have a Field Named X

E0026 means: you’re taking a struct or enum variant apart in a pattern, like Shape::Circle { r }, and one of the field names you wrote doesn’t exist. It’s the pattern version of E0560 and E0559, which catch the same mistake when you create a value. rustc usually tells you the right name in a help: line, so the fix is often a single word. (This page is part of Rust compiler errors explained.)

error[E0026]: variant `Shape::Circle` does not have a field named `r`
--> src/main.rs:8:25
|
8 | Shape::Circle { r } => 3.14 * r * r, // ❌ the field is called `radius`
| ^
| |
| variant `Shape::Circle` does not have this field
| help: `Shape::Circle` has a field named `radius`

This is the program that produces it:

enum Shape {
Circle { radius: f64 },
Rectangle { width: f64, height: f64 },
}
fn area(shape: &Shape) -> f64 {
match shape {
Shape::Circle { r } => 3.14 * r * r, // ❌ the field is called `radius`
Shape::Rectangle { width, height } => width * height,
}
}
fn main() {
println!("{}", area(&Shape::Circle { radius: 1.0 }));
}

You’ll also see a second error for the same line: E0027, “pattern does not mention field radius”. That’s the same mistake seen from the other side. Rust found a field you named that doesn’t exist (r) and a real field you didn’t name (radius). Fix the name and both errors go away.

The headline names the struct or variant you’re matching (Shape::Circle) and the field you wrote (r). The ^ points at that name inside the braces of the pattern.

In a pattern, the names inside { } are field names, not new variable names you get to choose. Shape::Circle { r } is short for Shape::Circle { r: r }: “take the field called r and put it in a variable called r”. Circle has no field called r, so Rust stops.

Under the arrow, rustc gives one of these hints:

  • help: `Shape::Circle` has a field named `radius`: there’s exactly one field you didn’t mention, so rustc guesses you meant that one.
  • help: a field with a similar name exists: a typo, like nmae for name. rustc shows the corrected line.
  • No hint at all: rustc couldn’t guess. Look at the struct or enum definition for the real names.

It’s like asking a librarian for “the book on the shelf called Fiction-ish”. They know you want some shelf, but they can only go to shelves that really exist.

A pattern like Shape::Circle { radius } is a promise: “when the value is a Circle, give me its radius”. Rust checks that promise against the definition when it compiles, so a pattern can never ask for data that isn’t there. See Destructuring tuples and structs.

In languages without this check, reading a missing key gives you undefined or null, and the bug shows up much later, far from the typo. In Rust the typo can’t get past the compiler. It also protects you when someone renames a field: every pattern that still uses the old name lights up with E0026, so none of them are silently forgotten.

Choose this most of the time. Use the name from the help: line, or look at the definition. Field names are case-sensitive and written in snake_case.

A typo gets the “similar name” hint and the corrected line:

error[E0026]: struct `User` does not have a field named `nmae`
--> src/main.rs:8:16
|
8 | let User { nmae, age } = user; // ❌ typo: the field is `name`
| ^^^^ struct `User` does not have this field
|
help: a field with a similar name exists
|
8 - let User { nmae, age } = user; // ❌ typo: the field is `name`
8 + let User { name, age } = user; // ❌ typo: the field is `name`
|

The program behind it:

struct User {
name: String,
age: u32,
}
fn main() {
let user = User { name: String::from("Ferris"), age: 7 };
let User { nmae, age } = user; // ❌ typo: the field is `name`
println!("{} is {} years old", nmae, age);
}

Write the field names exactly as the definition does. Then the shorthand works, and you get variables with the same names:

struct User {
name: String,
age: u32,
}
enum Shape {
Circle { radius: f64 },
Rectangle { width: f64, height: f64 },
}
fn area(shape: &Shape) -> f64 {
match shape {
Shape::Circle { radius } => 3.14 * radius * radius, // ✅ the real field name
Shape::Rectangle { width, height } => width * height,
}
}
fn main() {
let shapes = [
Shape::Circle { radius: 2.0 },
Shape::Rectangle { width: 3.0, height: 4.0 },
];
for shape in &shapes {
println!("area: {:.2}", area(shape));
}
let user = User { name: String::from("Ferris"), age: 7 };
let User { name, age } = user; // ✅ `name`, not `nmae`
println!("{} is {} years old", name, age);
}

Output:

area: 12.56
area: 12.00
Ferris is 7 years old
▶ Try it Yourself

Fix 2: Keep your own variable name with field: name

Section titled “Fix 2: Keep your own variable name with field: name”

Choose this when you like the short name r and just want the variable to be called that. Write the real field name, a colon, then your variable name: Shape::Circle { radius: r }. Read it as “take the radius field and call it r”.

enum Shape {
Circle { radius: f64 },
Rectangle { width: f64, height: f64 },
}
fn area(shape: &Shape) -> f64 {
match shape {
Shape::Circle { radius: r } => 3.14 * r * r, // ✅ field `radius`, variable `r`
Shape::Rectangle { width: w, height: h } => w * h,
}
}
fn main() {
let small = Shape::Circle { radius: 1.0 };
let wide = Shape::Rectangle { width: 10.0, height: 2.5 };
println!("small: {:.2}", area(&small));
println!("wide: {:.2}", area(&wide));
}

Output:

small: 3.14
wide: 25.00
▶ Try it Yourself

This is also handy when two fields would otherwise clash with variables you already have, or when the field name is long.

Fix 3: Check that you’re matching the right variant

Section titled “Fix 3: Check that you’re matching the right variant”

Choose this when the field you wrote is real, just in a different variant. Each variant of an enum has its own fields. Here x belongs to Click, but the arm matches KeyPress:

error[E0026]: variant `Event::KeyPress` does not have a field named `x`
--> src/main.rs:9:27
|
9 | Event::KeyPress { x, .. } => format!("key at {}", x), // ❌ `x` belongs to `Click`
| ^
| |
| variant `Event::KeyPress` does not have this field
| help: `Event::KeyPress` has a field named `key`

The program behind it:

enum Event {
Click { x: i32, y: i32 },
KeyPress { key: char },
}
fn describe(event: &Event) -> String {
match event {
Event::Click { x, y } => format!("click at ({}, {})", x, y),
Event::KeyPress { x, .. } => format!("key at {}", x), // ❌ `x` belongs to `Click`
}
}
fn main() {
println!("{}", describe(&Event::KeyPress { key: 'a' }));
}

rustc’s guess (key) is just “the only field you didn’t mention”, so don’t take it blindly. Ask yourself what this arm should do. A key press has no position, so here the right fix is to use the field KeyPress really has:

enum Event {
Click { x: i32, y: i32 },
KeyPress { key: char },
}
fn describe(event: &Event) -> String {
match event {
Event::Click { x, y } => format!("click at ({}, {})", x, y),
Event::KeyPress { key } => format!("key '{}' pressed", key), // ✅ KeyPress has `key`
}
}
fn main() {
let events = [Event::Click { x: 10, y: 20 }, Event::KeyPress { key: 'a' }];
for event in &events {
println!("{}", describe(event));
}
}

Output:

click at (10, 20)
key 'a' pressed
▶ Try it Yourself

If a key press really should have a position, add the fields to the variant instead: KeyPress { key: char, x: i32, y: i32 }. Then every place that creates a KeyPress must fill them in too (rustc points at each one with E0063).

After renaming a field. You renamed r to radius in the enum, and every Shape::Circle { r } => ... arm now gives E0026. Search the project for the old name and update each pattern, or use radius: r to keep the old variable name.

In if let and let. if let Some(User { nmae, .. }) = find_user() gives the same error. Adding .. doesn’t help here: .. skips fields you don’t name, but every name you do write must still exist. Fix: User { name, .. }.

Braces on a tuple variant or tuple struct. For struct Point(i32, i32);, the pattern Point { x, y } has no real field names to match. rustc reports that with a different code, E0769, “tuple variant Point written as struct variant”, and suggests Point(x, y). See Destructuring enums with data.

Quick check

1. For enum Shape { Circle { radius: f64 } }, what does the pattern Shape::Circle { r } ask for?

2. You want the radius field in a variable called r. Which pattern works?

3. Does User { nmae, .. } compile if the field is called name?

Score: 0 / 3