Rust Error E0559: Variant Has No Field Named X
E0559 means: you’re creating an enum value with Enum::Variant { field: value }, and one of the fields you wrote doesn’t exist in that variant. It’s the enum version of E0560, which is the same mistake for structs. Usually it’s a short or misspelled field name, a field that belongs to another variant, or braces on a variant that takes parentheses. The fix is almost always one word. (This page is part of Rust compiler errors explained.)
The error message
Section titled “The error message”error[E0559]: variant `Shape::Circle` has no field named `r` --> src/main.rs:8:33 |8 | let shape = Shape::Circle { r: 2.0 }; // ❌ the field is called `radius` | ^ `Shape::Circle` does not have this field | = note: available fields are: `radius`This is the program that produces it:
#[derive(Debug)]enum Shape { Circle { radius: f64 }, Rectangle { width: f64, height: f64 },}
fn main() { let shape = Shape::Circle { r: 2.0 }; // ❌ the field is called `radius` println!("{:?}", shape);}What it means
Section titled “What it means”The headline names the variant (Shape::Circle) and the field you wrote (r). The ^ points at that field inside the braces. Rust looked at the definition Circle { radius: f64 } and found no field called r.
Each variant of an enum has its own field list. Shape::Circle has radius, Shape::Rectangle has width and height. They don’t share fields, even inside the same enum.
Under the arrow, rustc gives one of these hints. Read it first, it usually contains the answer:
note: available fields are: `radius`: no close match. These are the fields this variant has that you haven’t filled in yet.help: a field with a similar name exists: a typo, likekyeforkey. rustc shows the corrected line.help: `Message::Write` is a tuple variant, use the appropriate syntax: the variant’s fields have no names, so it needs( ), not{ }.note: all struct fields are already assigned: you filled in every real field, and this one is extra.
It’s like ordering a pizza and asking for “large crust” when the menu only has “crust size”. The kitchen knows exactly what you mean, but the order form has no box with that name.
Why Rust does this
Section titled “Why Rust does this”An enum variant with named fields, like Circle { radius: f64 }, works like a small struct inside the enum. When you create one, Rust checks that you gave exactly its fields: none missing, none extra, none misspelled. See Enums can hold data.
This matters most when you match on the enum later. The arm Shape::Circle { radius } => ... can only work if every Circle really has a radius. If Rust let Shape::Circle { r: 2.0 } through, that guarantee would be gone. Catching the typo at compile time keeps every Circle in your program complete.
How to fix it
Section titled “How to fix it”Fix 1: Use the variant’s real field name
Section titled “Fix 1: Use the variant’s real field name”Choose this when rustc shows available fields are or a field with a similar name exists. Use the name from the hint. Field names are case-sensitive and written in snake_case, so Radius and radius are different.
A typo gets the “similar name” hint:
error[E0559]: variant `Event::KeyPress` has no field named `kye` --> src/main.rs:8:31 |8 | let e = Event::KeyPress { kye: 'a' }; | ^^^ unknown field |help: a field with a similar name exists |8 - let e = Event::KeyPress { kye: 'a' };8 + let e = Event::KeyPress { key: 'a' }; |A short name like r is too different from radius for a suggestion, so you get the list of fields instead. Either way, write the field the variant defines:
#[derive(Debug)]enum Shape { Circle { radius: f64 }, Rectangle { width: f64, height: f64 },}
#[derive(Debug)]enum Event { Click { x: i32, y: i32 }, KeyPress { key: char },}
fn main() { let shapes = [ Shape::Circle { radius: 2.0 }, // ✅ the real field name Shape::Rectangle { width: 3.0, height: 4.0 }, ]; for shape in &shapes { println!("{:?}", shape); }
let events = [Event::KeyPress { key: 'a' }, Event::Click { x: 10, y: 20 }]; println!("{:?}", events);}Output:
Circle { radius: 2.0 }Rectangle { width: 3.0, height: 4.0 }[KeyPress { key: 'a' }, Click { x: 10, y: 20 }]Watch out for the field init shorthand. Shape::Circle { r } means Shape::Circle { r: r }, so it gives the same E0559. If your variable is called r, write the field out: Shape::Circle { radius: r }. Or rename the variable to radius and write Shape::Circle { radius }.
Fix 2: Use parentheses for a tuple variant
Section titled “Fix 2: Use parentheses for a tuple variant”Choose this when rustc says is a tuple variant, use the appropriate syntax. A variant like Write(String) has a field, but the field has no name. So any name you write in braces is wrong:
error[E0559]: variant `Message::Write` has no field named `text` --> src/main.rs:8:30 |4 | Write(String), | ----- `Message::Write` defined here...8 | let m = Message::Write { text: String::from("hi") }; | ^^^^ field does not exist |help: `Message::Write` is a tuple variant, use the appropriate syntax |8 - let m = Message::Write { text: String::from("hi") };8 + let m = Message::Write(/* String */); |The /* String */ in the suggestion is a placeholder: put your value there. Create a tuple variant with parentheses, in the same order as its definition:
#[derive(Debug)]enum Message { Move { x: i32, y: i32 }, // named fields: use { } Write(String), // unnamed field: use ( ) Color(u8, u8, u8),}
fn main() { let messages = vec![ Message::Move { x: 1, y: 2 }, Message::Write(String::from("hi")), // ✅ parentheses Message::Color(255, 128, 0), ]; for m in &messages { match m { Message::Move { x, y } => println!("move to ({}, {})", x, y), Message::Write(text) => println!("write: {}", text), Message::Color(r, g, b) => println!("color: {} {} {}", r, g, b), } }}Output:
move to (1, 2)write: hicolor: 255 128 0If you’d rather name the field, change the definition to Write { text: String }. Then Message::Write { text: ... } works. The opposite mix-up, parentheses on a variant with named fields like Shape::Circle(2.0), gives a different error: E0533, “expected value, found struct variant”.
Fix 3: Pick the right variant, or change the variant
Section titled “Fix 3: Pick the right variant, or change the variant”Choose this when the field you wrote exists, just in a different variant. Here radius belongs to Circle, not Square:
error[E0559]: variant `Shape::Square` has no field named `radius` --> src/main.rs:8:29 |8 | let s = Shape::Square { radius: 2.0 }; | ^^^^^^ `Shape::Square` does not have this field | = note: available fields are: `side`Ask yourself which one you meant. If you wanted a circle, use Shape::Circle { radius: 2.0 }. If you wanted a square, use its field: Shape::Square { side: 2.0 }.
If you see note: all struct fields are already assigned, the variant is complete and your field is extra. Remove it, or, if your code really needs it, add it to the variant’s definition:
#[derive(Debug)]enum Shape { Circle { radius: f64 }, Square { side: f64 },}
#[derive(Debug)]enum Message { Move { x: i32, y: i32, z: i32 }, // `z` added, because the code needs it}
fn main() { let a = Shape::Circle { radius: 2.0 }; // a circle has a radius let b = Shape::Square { side: 2.0 }; // a square has a side println!("{:?} and {:?}", a, b);
let m = Message::Move { x: 1, y: 2, z: 3 }; println!("{:?}", m);}Output:
Circle { radius: 2.0 } and Square { side: 2.0 }Move { x: 1, y: 2, z: 3 }When you add a field to a variant, every place that creates that variant must fill it in too. rustc points at each one with E0063, “missing field”.
Where you’ll usually see it
Section titled “Where you’ll usually see it”Constructor methods with Self::. Inside impl Shape, writing fn circle(r: f64) -> Shape { Self::Circle { r } } gives E0559 for r, because of the shorthand. Fix: Self::Circle { radius: r }. See Enums can have methods too.
After renaming a field. You renamed Circle { r } to Circle { radius } in the enum, and every old Shape::Circle { r: ... } now gives E0559 with available fields are: `radius`. Search the project for the old name and update each one.
In a match pattern. Writing the wrong field name in a pattern, like Shape::Circle { r } => ..., is the same mistake, but rustc uses other codes: E0026, “variant Shape::Circle does not have a field named r”, with help: `Shape::Circle` has a field named `radius`, plus E0027 “pattern does not mention field radius”. Fix: Shape::Circle { radius } => ..., or Shape::Circle { radius: r } => ... to keep the short name. See Pattern matching.
Related errors
Section titled “Related errors”- E0560: struct has no field named x: the same mistake for a struct literal, like
User { nmae: ... }. - E0063: missing field in initializer: the opposite, a field the variant has that you didn’t fill in.
- E0599: no method named x found: rustc uses the same code when the variant name is wrong, like
Shape::Circel { ... }. rustc says “no variant namedCircelfound for enumShape”. - E0609: no field on type: a wrong field name when reading a field.
E0026: variant does not have a field named x: the same mistake inside amatchpattern (see above).E0533: expected value, found struct variant: parentheses on a variant with named fields, likeShape::Circle(2.0).- All compiler errors explained
Quick check
Section titled “Quick check”Quick check
1. For enum Message { Write(String) }, what does Message::Write { text: s } need to become?
2. You have let r = 2.0; and write Shape::Circle { r }, but the field is radius. What's the fix?
3. Shape::Square { radius: 2.0 } gives E0559, and the note lists only side. What's going on?
Score: 0 / 3
Read more
Section titled “Read more”- E0559 — Rust error codes index: the official explanation of this error.
- Defining an Enum — The Rust Book: variants with named fields, tuple variants and unit variants, with the
Messageexample. - Enums — Rust by Example: runnable examples of every kind of variant.
- Struct expressions — The Rust Reference: the exact rules for
Enum::Variant { field: value }, including the shorthand. - E0026 — Rust error codes index: the official page for the same mistake inside a pattern.