error[E0063]: missing field in initializer
You created a struct without giving a value to one of its fields. Every field must be set: Rust has no null and no implicit default values.
Quick fix: Add the missing field, or derive Default and finish the literal with ..Default::default().
Code that triggers E0063
struct User {
name: String,
email: String,
active: bool,
}
fn main() {
let user = User {
name: String::from("Ferris"),
email: String::from("[email protected]"),
};
println!("{} <{}> {}", user.name, user.email, user.active);
}What the compiler says
error[E0063]: missing field `active` in initializer of `User`
--> src/main.rs:8:16
|
8 | let user = User {
| ^^^^ missing `active`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0063`.Why Rust raises E0063
A struct literal must provide every field. Leaving out active is an error rather than a silent false, because an uninitialised field would be undefined behaviour waiting to happen. The message names the missing field, and lists all of them if several are missing.
How to fix E0063
1. Provide the missing field
Set every field in the struct literal.
struct User {
name: String,
email: String,
active: bool,
}
fn main() {
let user = User {
name: String::from("Ferris"),
email: String::from("[email protected]"),
active: true,
};
println!("{} <{}> {}", user.name, user.email, user.active);
}2. Fill the rest from Default
Derive Default and finish with struct update syntax, ..Default::default(). Any field you don’t set gets its default: an empty String, false for bool, 0 for numbers.
#[derive(Default)]
struct User {
name: String,
email: String,
active: bool,
}
fn main() {
let user = User {
name: String::from("Ferris"),
..Default::default()
};
println!("{} <{}> {}", user.name, user.email, user.active);
}Related errors
- E0609: no field on type
- E0594: cannot assign, as it is not declared as mutable
- E0599: no method named
push_strfound for reference&str
Learn the concept behind it
The Structs lesson explains the rules behind E0063 with diagrams and more examples. For the official reference, run rustc --explain E0063 or read the E0063 entry in the Rust error index .
Hit a different error? Paste it into the Rust error lookup to find its fix.