error[E0609]: no field on type
You accessed a field that doesn’t exist on that type. Usually it’s a typo, or the value is an Option or Result wrapping the struct you meant.
Quick fix: Use a field name from the available fields note, or unwrap the Option or Result before reading the field.
Code that triggers E0609
struct User {
name: String,
active: bool,
}
fn main() {
let user = User { name: String::from("Ferris"), active: true };
println!("{} {}", user.username, user.active);
}What the compiler says
error[E0609]: no field `username` on type `User`
--> src/main.rs:8:28
|
8 | println!("{} {}", user.username, user.active);
| ^^^^^^^^ unknown field
|
= note: available fields are: `name`, `active`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0609`.Why Rust raises E0609
User has no field called username. The compiler’s note lists the fields that do exist, so typos and renamed fields are quick to fix.
The second classic cause: the value isn’t the struct you think it is. A lookup that can fail returns Option<User>, and fields only exist on the User inside it.
Here find_user returns Option<User>, so user.name fails:
struct User {
name: String,
}
fn find_user(id: u32) -> Option<User> {
(id == 1).then(|| User { name: String::from("Ferris") })
}
fn main() {
let user = find_user(1);
println!("{}", user.name);
}error[E0609]: no field `name` on type `Option<User>`
--> src/main.rs:11:25
|
11 | println!("{}", user.name);
| ^^^^ unknown field
|
help: one of the expressions' fields has a field of the same name
|
11 | println!("{}", user.unwrap().name);
| +++++++++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0609`.How to fix E0609
1. Use the field that exists
Use one of the field names from the note.
struct User {
name: String,
active: bool,
}
fn main() {
let user = User { name: String::from("Ferris"), active: true };
println!("{} {}", user.name, user.active);
}2. Get the struct out of the Option first
Handle the None case with if let, match or the ? operator, then read the field from the User inside. Avoid .unwrap() in real code unless a missing value is truly impossible.
struct User {
name: String,
}
fn find_user(id: u32) -> Option<User> {
(id == 1).then(|| User { name: String::from("Ferris") })
}
fn main() {
if let Some(user) = find_user(1) {
println!("{}", user.name);
}
}Related errors
- E0599: no method named
push_strfound for reference&str - E0063: missing field in initializer
- E0425: cannot find value in this scope
Learn the concept behind it
The Structs lesson explains the rules behind E0609 with diagrams and more examples. For the official reference, run rustc --explain E0609 or read the E0609 entry in the Rust error index .
Hit a different error? Paste it into the Rust error lookup to find its fix.