Skip to content

Rust Error E0560: Struct Has No Field Named X

E0560 means: you’re creating a struct with Name { field: value }, and one of the fields you wrote doesn’t exist in that struct. Usually it’s a typo, a field that was renamed, or a field that belongs to a different struct. rustc often tells you the exact name you meant, so this is one of the quickest errors to fix. (This page is part of Rust compiler errors explained.)

error[E0560]: struct `User` has no field named `nmae`
--> src/main.rs:9:9
|
9 | nmae: String::from("Ferris"), // ❌ typo: the field is `name`
| ^^^^ unknown field
|
help: a field with a similar name exists
|
9 - nmae: String::from("Ferris"), // ❌ typo: the field is `name`
9 + name: String::from("Ferris"), // ❌ typo: the field is `name`
|

This is the program that produces it:

struct User {
name: String,
email: String,
active: bool,
}
fn main() {
let user = User {
nmae: String::from("Ferris"), // ❌ typo: the field is `name`
email: String::from("[email protected]"),
active: true,
};
println!("{}", user.email);
}

The headline names the struct (User) and the field you wrote (nmae). The ^^^^ points at that field inside the User { ... } block. Rust compared it with the field list in struct User and found no match.

Under the arrow you’ll get one of three hints. Each one tells you something different:

  • help: a field with a similar name exists: a typo. rustc shows the line with the right name.
  • note: available fields are: `username`: no close match. These are the fields you haven’t filled in yet.
  • note: all struct fields are already assigned: you filled in every real field, and this one is extra.

Notice that rustc doesn’t also complain that name is missing. Fix the typo and both problems go away at once.

It’s like filling in a paper form and writing your phone number in a box called “Fax”. The form has no such box, so the clerk hands it back.

A struct’s fields are fixed when you write struct User { ... }. Every time you create a User, Rust checks that you gave exactly those fields: none missing, none extra, none misspelled. See Defining and creating a struct.

This catches real bugs. In JavaScript, { nmae: "Ferris" } silently creates an object with the wrong key, and user.name is undefined somewhere much later. In Rust, the typo can’t get past the compiler, so every User in your program really has a name.

Choose this when you see a field with a similar name exists. Use the name from the help: line. Field names are case-sensitive, and Rust uses snake_case, so userName and user_name are different names.

Watch out for the field init shorthand too: User { nmae, email } means User { nmae: nmae, email: email }, so the variable name must match the field name. Either rename the variable or write the field out: User { name: nmae, email }.

struct User {
name: String,
email: String,
active: bool,
}
fn main() {
let user = User {
name: String::from("Ferris"), // ✅ the real field name
email: String::from("[email protected]"),
active: true,
};
println!("{} ({}) active: {}", user.name, user.email, user.active);
// shorthand: the variable names match the field names
let name = String::from("Ada");
let email = String::from("[email protected]");
let other = User { name, email, active: false };
println!("{} ({}) active: {}", other.name, other.email, other.active);
}

Output:

Ferris ([email protected]) active: true
Ada ([email protected]) active: false
▶ Try it Yourself

Fix 2: Use the field’s new name, or add the field

Section titled “Fix 2: Use the field’s new name, or add the field”

Choose this when there’s no “similar name” hint. Often the struct changed: someone renamed name to username, or a field was removed, but this code still uses the old one. rustc lists what’s left:

error[E0560]: struct `User` has no field named `name`
--> src/main.rs:8:9
|
8 | name: String::from("Ferris"),
| ^^^^ `User` does not have this field
|
= note: available fields are: `username`

You have two honest choices. If the struct is right, use its current field name. If your code is right and the struct really needs the field, add it to the struct definition. Here Config gave all struct fields are already assigned for debug, and the fix is to add debug to the struct:

struct User {
username: String, // renamed from `name`: use the new name
email: String,
}
struct Config {
port: u16,
host: String,
debug: bool, // added to the struct, because the code needs it
}
fn main() {
let user = User {
username: String::from("ferris"),
email: String::from("[email protected]"),
};
println!("{} ({})", user.username, user.email);
let config = Config {
port: 8080,
host: String::from("localhost"),
debug: true,
};
println!("{}:{} debug={}", config.host, config.port, config.debug);
}

Output:

localhost:8080 debug=true
▶ Try it Yourself

If the struct comes from a crate you just updated, the field may have been renamed in the new version. Check the struct on docs.rs for that version, or the crate’s changelog. Once you add a field to your own struct, every place that creates it must fill it in too. rustc will point at each one with E0063 (see below).

Choose this when the field you wrote exists, just in a different struct. With two similar structs, like a Product with a title and a Customer with a name, it’s easy to mix them up:

error[E0560]: struct `Product` has no field named `name`
--> src/main.rs:13:9
|
13 | name: String::from("Keyboard"),
| ^^^^ `Product` does not have this field
|
= note: available fields are: `title`

Look at the available fields note. If it lists fields you didn’t expect, you’re building the wrong struct. Use the right struct name, or the field name of the struct you meant:

struct Product {
title: String,
price: u32, // in cents
}
struct Customer {
name: String,
email: String,
}
fn main() {
let keyboard = Product {
title: String::from("Keyboard"), // products have a `title`
price: 4900,
};
let buyer = Customer {
name: String::from("Ana"), // customers have a `name`
email: String::from("[email protected]"),
};
println!("{} buys a {} for {} cents", buyer.name, keyboard.title, keyboard.price);
println!("Receipt sent to {}", buyer.email);
}

Output:

Ana buys a Keyboard for 4900 cents
Receipt sent to [email protected]
▶ Try it Yourself

Choose this when the struct is a tuple struct, like struct Point(i32, i32);. Its fields have no names, so Point { x: 1, y: 2 } gives E0560 for both x and y, with help: `Point` is a tuple struct, use the appropriate syntax and the suggestion Point(/* i32 */, /* i32 */). Create it with parentheses, Point(1, 2), and read it with .0 and .1. If you’d rather have names, change the definition to struct Point { x: i32, y: i32 }. See When to use a tuple vs a struct.

The twin of E0560 is E0063: a field the struct has, but you didn’t fill in. Rust never fills fields in for you, so every field needs a value:

error[E0063]: missing field `active` in initializer of `User`
--> src/main.rs:8:16
|
8 | let user = User {
| ^^^^ missing `active`

With several fields missing, it lists them all: missing fields `active`, `email` and `login_count` in initializer of `User`. Fix: give every field a value. If many fields have sensible defaults, derive Default and use the struct update syntax .. to fill in the rest:

#[derive(Debug, Default)]
struct Settings {
volume: u8,
dark_mode: bool,
language: String,
}
fn main() {
// every field written out
let full = Settings { volume: 80, dark_mode: false, language: String::from("en") };
// only the fields you care about, the rest from Default
let quick = Settings { dark_mode: true, ..Default::default() };
// copy the rest from another value
let louder = Settings { volume: 100, ..full };
println!("{:?}", quick);
println!("{:?}", louder);
}

Output:

Settings { volume: 0, dark_mode: true, language: "" }
Settings { volume: 100, dark_mode: false, language: "en" }
▶ Try it Yourself

The default for a number is 0, for bool it’s false, and for String it’s empty. If you want other defaults, write impl Default for Settings yourself. See Default: a sensible starting value.

Inside .. update syntax. Settings { dark: true, ..defaults } gives E0560 too. Here the note lists all fields (available fields are: `volume`, `dark_mode`, `language`), and there may be no “similar name” hint when the names are too different. Fix: dark_mode: true.

Enum variants with named fields. For enum Shape { Circle { radius: f64 } }, writing Shape::Circle { r: 2.0 } is the same mistake, but rustc uses a different code: E0559, variant `Shape::Circle` has no field named `r`, with the same available fields note and typo hints. Fix: Shape::Circle { radius: 2.0 }. See Enums can hold data.

A struct from another module. If the field exists but isn’t pub, you get E0451, “field balance of struct Account is private”, instead. Fix: use the module’s constructor, like Account::new(...). See Everything is private by default.

Quick check

1. rustc says note: all struct fields are already assigned. What does it mean?

2. You have let nmae = ...; and write User { nmae, email }. How do you fix it?

3. Which error do you get when you leave a field out, like User { name, email } without active?

Score: 0 / 3