Rust errors / Names, paths and imports
Rust compiler error · checked with rustc 1.99.0
Rust error E0027: pattern does not mention field `name`
What it meansA pattern for a struct fails to specify a sub-pattern for every one of the struct's fields.
What rustc prints
error[E0027]: pattern does not mention field `name` --> main.rs:12:5 | 12 | Dog { age: x } => {} | ^^^^^^^^^^^^^^ missing field `name` | help: include the missing field in the pattern | 12 | Dog { age: x, name } => {} | ++++++ help: if you don't care about this missing field, you can explicitly ignore it | 12 | Dog { age: x, name: _ } => {} | +++++++++ help: or always ignore missing fields here | 12 | Dog { age: x, .. } => {} | ++++
Real output of rustc 1.99.0 compiling the official example below (2021 edition). Your names and line numbers will differ; the first line is the part to match.
The compiler team's explanation
A pattern for a struct fails to specify a sub-pattern for every one of the struct's fields.
Erroneous code example:
struct Dog {
name: String,
age: u32,
}
let d = Dog { name: "Rusty".to_string(), age: 8 };
// This is incorrect.
match d {
Dog { age: x } => {}
}
To fix this error, ensure that each field from the struct's definition is mentioned in the pattern, or use .. to ignore unwanted fields. Example:
struct Dog {
name: String,
age: u32,
}
let d = Dog { name: "Rusty".to_string(), age: 8 };
match d {
Dog { name: ref n, age: x } => {}
}
// This is also correct (ignore unused fields).
match d {
Dog { age: x, .. } => {}
}From rustc --explain E0027 (1.99.0), part of the Rust project, MIT or Apache-2.0. Official page.
Related errors
Up next
Often hit nextE0425: cannot find value in this scopeA related error from the same family.Read it next →Did this answer your question?