Rust
E0063 Missing Fields in Struct Fix
E0063 is the incomplete-initializer error: a struct expression names some of a type's fields and leaves the rest out, and the compiler will not guess the missing values. This guide separates the eleven shapes that produce it, matches each one to the repair that fits it - name the field, fill from Default, or update from a complete instance - and draws the line against the neighboring initializer errors that look similar but are not this one.
Written November 25, 2026 · Tested with Rust 1.99.1 · 31 min read
- The rule underneath: every field, exactly once, or a named source for the rest
- How to read the diagnostic
- The first failure: one field missing from a two-field struct
- The second failure: one field missing from a four-field record
Written November 25, 2026
Tested with Rust 1.99.1
What this guide is
Error E0063 is the missing-field error. The error codes index gives it one sentence - a struct's or struct-like enum variant's field was not provided - and one example: a two-field struct Foo built as Foo { x: 0 }, with the comment that y is missing. Every failure in this guide has that same skeleton. A struct expression names a subset of the fields, the type declares more fields than the expression provides, and there is no source for the remaining values - so the value cannot be built.
The rule underneath is the one the index states as its repair: each field should be specified exactly once. A struct expression is a complete construction, not a partial one with defaults implied. The Book's struct chapter introduces instantiation the same way: state the name of the struct, then give key-value pairs for the fields - all of them. Where the language does allow a partial list, it says so explicitly, and there are exactly two such mechanisms: struct update syntax, which takes the remaining fields from another instance of the same type, and a ..Default::default() fill, which takes them from the type's Default implementation. Both are struct mechanisms - functional update syntax does not exist for struct-like enum variants, whose missing fields can only be supplied by name. Both appear below as repairs, and both fail in their own ways when their preconditions are missing - those failures are in the traps, with their own codes.
The repairs fall into three groups. Name the missing field with a value - the direct repair, and the only one that always applies. Fill the rest from Default, when the type implements it and a default value is what the field should hold. Or build from a complete instance with struct update syntax, when one is at hand and its remaining values are the ones wanted. Which repair fits depends on where the missing value should come from, and the diagnostic names the field and the type - so reading it comes first.
Build every program in this guide with rustc --edition 2021. In the compiler transcripts below, the file names are the bare names shown on the command line.
The rule underneath: every field, exactly once, or a named source for the rest
The Reference describes a struct expression as a path to a struct, enum variant, or union followed by the values for its fields, with each field's name separated from its value by a colon, in any order. Nothing in that description makes a field optional. A field the expression does not name has no value, and a struct value with a hole in it cannot exist - every later read of that field would read uninitialized storage. The compiler therefore rejects the expression at the point of construction and names what is missing.
The two exceptions are both visible in the expression itself. A struct expression that constructs a value of a struct type can terminate with .. followed by a base expression of the same type - the functional update the Reference describes - and the expression then uses the given values for the fields that were specified and moves or copies the remaining fields from the base. ..Default::default() is that same mechanism with a freshly built default value as the base. Neither form is available on a struct-like enum variant: the same .. clause there is error E0436, functional record update syntax requires a struct, so a variant's fields must all be named. In both cases the remaining fields do have a source, written in the code, and the compiler can check it: the base must be the same type, and a Default base requires the type to implement Default. Field init shorthand, also from the Reference - writing fieldname for fieldname: fieldname - is not an exception at all: the field is still specified, just compactly, and leaving the name out entirely is still E0063.
Two consequences follow, and both appear below. First, the error is about the expression, not the position it sits in: the same missing field is E0063 in a let, in a return expression, in an array element, and inside a nested initializer, because in each case the struct expression itself is incomplete. Second, several missing fields are one error, not several: the diagnostic lists every missing field in a single message, so a count of errors is not a count of missing fields - the list in the message is.
How to read the diagnostic
Every family in this guide prints the same header - error E0063, "missing field" (or "missing fields") in the initializer of the type - with the span on the struct expression and one label naming what is missing. The first family's, in full:
$ rustc --edition 2021 fail_two_fields.rs -o fail_two_fields
error[E0063]: missing field `y` in initializer of `Foo`
--> fail_two_fields.rs:3:13
|
3 | let f = Foo { x: 1 };
| ^^^ missing `y`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0063`.
Read it in order. The header names the missing field and the type being built - here, field y of Foo. The span covers the whole struct expression, not the place where a value should be inserted, and the label under it repeats the missing name: "missing y". When more than one field is missing, the header and the label list all of them in the one error, as the eleventh family shows. There are no notes and no suggested values after the label in any family in this guide: the compiler knows the field's name and type from the definition, but a value is the program's decision, and the diagnostic does not offer one. The reading above is scoped to the forms printed in this guide.
The first failure: one field missing from a two-field struct
The example from the error codes index, with values that are read back:
struct Foo { x: i32, y: i32 }
fn main() {
let f = Foo { x: 1 };
println!("{} {}", f.x, f.y);
}
$ rustc --edition 2021 fail_two_fields.rs -o fail_two_fields
error[E0063]: missing field `y` in initializer of `Foo`
--> fail_two_fields.rs:3:13
|
3 | let f = Foo { x: 1 };
| ^^^ missing `y`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0063`.
struct Foo { x: i32, y: i32 }
fn main() {
let f = Foo { x: 1, y: 2 };
println!("{} {}", f.x, f.y);
}
$ rustc --edition 2021 fix_two_fields.rs -o fix_two_fields
$ ./fix_two_fields
1 2
Foo declares x and y; the expression provides x alone. There is no second source in the expression - no .. base, no default - so y has no value and the construction is refused. The fix is the index's own example completed: name y with its value, and the program runs.
The output is 1 2, the two fields read back. Nothing else changes between the two programs - the definition, the read, and the provided field are identical; only the missing field is supplied.
The second failure: one field missing from a four-field record
A wider struct, built the way the Book's User example is built, with one field left out:
struct User { active: bool, username: String, email: String, sign_in_count: u64 }
fn main() {
let u = User { active: true, username: String::from("someusername123"), sign_in_count: 1 };
println!("{} {} {} {}", u.active, u.username, u.email, u.sign_in_count);
}
$ rustc --edition 2021 fail_many_fields.rs -o fail_many_fields
error[E0063]: missing field `email` in initializer of `User`
--> fail_many_fields.rs:3:13
|
3 | let u = User { active: true, username: String::from("someusername123"), sign_in_count: 1 };
| ^^^^ missing `email`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0063`.
struct User { active: bool, username: String, email: String, sign_in_count: u64 }
fn main() {
let u = User { active: true, username: String::from("someusername123"), email: String::from("someone@example.com"), sign_in_count: 1 };
println!("{} {} {} {}", u.active, u.username, u.email, u.sign_in_count);
}
$ rustc --edition 2021 fix_many_fields.rs -o fix_many_fields
$ ./fix_many_fields
true someusername123 someone@example.com 1
Three of the four fields are named, in an order that differs from the declaration - order is not the problem, and the diagnostic does not mention it. The missing field is email, and it is the only defect: the header names it alone. Width does not change the error's shape; a four-field struct missing one field and a two-field struct missing one field produce the same single-line diagnosis, because the compiler lists what is missing rather than grading how much of the struct was provided.
The fix names email with a String built the same way as the other text fields. The output is the four fields read back - true, the username, the email address, and the count - confirming the completed value, not just its construction.
The third failure: a field missing from a struct-like enum variant
The index's definition covers struct-like enum variants as well as structs, and the variant form fails identically:
enum Shape { Rect { w: i32, h: i32 }, Unit }
fn main() {
let s = Shape::Rect { w: 3 };
let t = Shape::Unit;
match t { Shape::Rect { w, h } => println!("{} {}", w, h), Shape::Unit => println!("unit") }
match s { Shape::Rect { w, h } => println!("{} {}", w, h), Shape::Unit => println!("unit") }
}
$ rustc --edition 2021 fail_enum_variant.rs -o fail_enum_variant
error[E0063]: missing field `h` in initializer of `Shape`
--> fail_enum_variant.rs:3:13
|
3 | let s = Shape::Rect { w: 3 };
| ^^^^^^^^^^^ missing `h`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0063`.
enum Shape { Rect { w: i32, h: i32 }, Unit }
fn main() {
let s = Shape::Rect { w: 3, h: 4 };
let t = Shape::Unit;
match t { Shape::Rect { w, h } => println!("{} {}", w, h), Shape::Unit => println!("unit") }
match s { Shape::Rect { w, h } => println!("{} {}", w, h), Shape::Unit => println!("unit") }
}
$ rustc --edition 2021 fix_enum_variant.rs -o fix_enum_variant
$ ./fix_enum_variant
unit
3 4
Shape::Rect declares w and h, and the expression provides w alone. The header names the type as Shape and the missing field as h - the variant is built by the same struct-expression machinery as a plain struct, so the same completeness rule applies. A unit variant such as Shape::Unit has no fields and cannot raise this error; the second value in each program builds one and matches it, printing unit before the rectangle's dimensions in the fixed program.
The fix names h. The output is unit from the unit value, then 3 4, the rectangle's two fields bound by the match and printed.
The fourth failure: a field missing from a nested struct
The incomplete expression sits inside a complete one:
struct Inner { a: i32, b: i32 }
struct Outer { inner: Inner, name: String }
fn main() {
let o = Outer { inner: Inner { a: 1 }, name: String::from("n") };
println!("{} {} {}", o.inner.a, o.inner.b, o.name);
}
$ rustc --edition 2021 fail_nested_inner.rs -o fail_nested_inner
error[E0063]: missing field `b` in initializer of `Inner`
--> fail_nested_inner.rs:4:28
|
4 | let o = Outer { inner: Inner { a: 1 }, name: String::from("n") };
| ^^^^^ missing `b`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0063`.
struct Inner { a: i32, b: i32 }
struct Outer { inner: Inner, name: String }
fn main() {
let o = Outer { inner: Inner { a: 1, b: 2 }, name: String::from("n") };
println!("{} {} {}", o.inner.a, o.inner.b, o.name);
}
$ rustc --edition 2021 fix_nested_inner.rs -o fix_nested_inner
$ ./fix_nested_inner
1 2 n
The outer expression provides both of Outer's fields - inner and name - and is itself complete. The defect is one level down: the Inner expression provides a and leaves out b, and the diagnostic points at the inner expression, naming Inner as the type and b as the missing field. Completeness is judged per expression, not per statement: an outer initializer cannot lend fields to an inner one, and a complete outer list does not excuse an incomplete inner list.
The fix names b inside the inner expression. The output is 1 2 n - the inner struct's two fields read through the outer one, then the outer name.
The fifth failure: a field missing from a generic struct
A generic parameter does not change the completeness rule:
struct Pair<T> { first: T, second: T }
fn main() {
let p = Pair { first: 1 };
println!("{} {}", p.first, p.second);
}
$ rustc --edition 2021 fail_generic.rs -o fail_generic
error[E0063]: missing field `second` in initializer of `Pair<_>`
--> fail_generic.rs:3:13
|
3 | let p = Pair { first: 1 };
| ^^^^ missing `second`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0063`.
struct Pair<T> { first: T, second: T }
fn main() {
let p = Pair { first: 1, second: 2 };
println!("{} {}", p.first, p.second);
}
$ rustc --edition 2021 fix_generic.rs -o fix_generic
$ ./fix_generic
1 2
Pair<T> declares first and second, and the expression provides first alone. The missing field is named exactly as for a concrete struct; the generic parameter is irrelevant to the diagnosis because field completeness is a property of the definition's shape, not of the type the parameter takes. The same shape with a String parameter in place of i32 fails the same way.
The fix names second. The output is 1 2.
The sixth failure: a field missing from a struct in another module
Visibility satisfied, completeness still required:
mod m { pub struct S { pub a: i32, pub b: i32 } }
fn main() {
let s = m::S { a: 1 };
println!("{} {}", s.a, s.b);
}
$ rustc --edition 2021 fail_cross_module.rs -o fail_cross_module
error[E0063]: missing field `b` in initializer of `S`
--> fail_cross_module.rs:3:13
|
3 | let s = m::S { a: 1 };
| ^^^^ missing `b`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0063`.
mod m { pub struct S { pub a: i32, pub b: i32 } }
fn main() {
let s = m::S { a: 1, b: 2 };
println!("{} {}", s.a, s.b);
}
$ rustc --edition 2021 fix_cross_module.rs -o fix_cross_module
$ ./fix_cross_module
1 2
Both of S's fields are pub, so naming them from main is permitted - and the expression names only a. The error is E0063, not a visibility error: the compiler can see the field, the program simply did not provide it. The two checks are separate, and this is the order they present in when only completeness fails. Where a field is private as well as missing, the presentation differs - a private field named in the expression is a visibility error instead, and it is in the traps.
The fix names b. The output is 1 2.
The seventh failure: a missing field the repair fills from Default
The same missing field, on a type that derives Default - where naming a value is not the only repair:
#[derive(Default)]
struct Opt { value: Option<i32>, name: String }
fn main() {
let o = Opt { name: String::from("n") };
println!("{} {:?}", o.name, o.value);
}
$ rustc --edition 2021 fail_option_default.rs -o fail_option_default
error[E0063]: missing field `value` in initializer of `Opt`
--> fail_option_default.rs:4:13
|
4 | let o = Opt { name: String::from("n") };
| ^^^ missing `value`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0063`.
#[derive(Default)]
struct Opt { value: Option<i32>, name: String }
fn main() {
let o = Opt { name: String::from("n"), ..Default::default() };
println!("{} {:?}", o.name, o.value);
}
$ rustc --edition 2021 fix_option_default.rs -o fix_option_default
$ ./fix_option_default
n None
The failing program is unchanged in shape: Opt declares value and name, and the expression provides name alone. Deriving Default does not make the field optional - the derive supplies a way to build a complete value, not a rule that incomplete expressions fall back to it, and the expression without a .. clause is refused exactly as before. What the derive changes is the repair menu.
The fix adds ..Default::default() as the base, and the missing value is taken from the default value - None, for an Option field. The output is n None: the named field keeps its given value, and the filled field holds the default. This repair is the right one only when the default is genuinely the value wanted; the traps show what happens when the type has no Default to fill from.
The eighth failure: a missing field the repair takes from a complete instance
A struct whose missing field is a collection, repaired with struct update syntax:
struct Bag { items: Vec<i32>, label: String }
fn main() {
let b = Bag { label: String::from("x") };
println!("{} {}", b.label, b.items.len());
}
$ rustc --edition 2021 fail_vec_field.rs -o fail_vec_field
error[E0063]: missing field `items` in initializer of `Bag`
--> fail_vec_field.rs:3:13
|
3 | let b = Bag { label: String::from("x") };
| ^^^ missing `items`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0063`.
struct Bag { items: Vec<i32>, label: String }
fn main() {
let base = Bag { items: vec![1, 2], label: String::from("x") };
let b = Bag { label: String::from("y"), ..base };
println!("{} {}", b.label, b.items.len());
}
$ rustc --edition 2021 fix_vec_field.rs -o fix_vec_field
$ ./fix_vec_field
y 2
The failing expression provides label and leaves out items. The fix does not name items at all: it first builds a complete Bag, then builds the wanted value as Bag { label: ..., ..base }, and the Reference's functional update takes the unspecified items field from base - moved, in this case, since a Vec does not copy. The explicitly named label overrides the base's.
The output is y 2: the new label, and the length of the vector that came from the base instance. Update syntax is the repair when a complete instance with the right remaining values already exists; naming the field is the repair when it does not.
The ninth failure: a missing field in a return position
The incomplete expression is a function's return value:
struct Foo { x: i32, y: i32 }
fn make() -> Foo {
Foo { x: 1 }
}
fn main() {
let f = make();
println!("{} {}", f.x, f.y);
}
$ rustc --edition 2021 fail_return_position.rs -o fail_return_position
error[E0063]: missing field `y` in initializer of `Foo`
--> fail_return_position.rs:3:5
|
3 | Foo { x: 1 }
| ^^^ missing `y`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0063`.
struct Foo { x: i32, y: i32 }
fn make() -> Foo {
Foo { x: 1, y: 2 }
}
fn main() {
let f = make();
println!("{} {}", f.x, f.y);
}
$ rustc --edition 2021 fix_return_position.rs -o fix_return_position
$ ./fix_return_position
1 2
make promises a Foo and its body ends with an expression that builds only half of one. The diagnostic points at the expression inside make, not at the call in main and not at the return type - the defect is located where the incomplete value is written, and the function's signature is not in question. Position does not matter to this error: a struct expression is checked where it stands.
The fix names y in the returned expression. The output is 1 2, read back from the returned value in main.
The tenth failure: a missing field in one element of an array
Two elements of the same type, one complete and one not:
struct Foo { x: i32, y: i32 }
fn main() {
let arr = [Foo { x: 1, y: 2 }, Foo { x: 3 }];
println!("{} {}", arr[1].x, arr[1].y);
}
$ rustc --edition 2021 fail_array_of_structs.rs -o fail_array_of_structs
error[E0063]: missing field `y` in initializer of `Foo`
--> fail_array_of_structs.rs:3:36
|
3 | let arr = [Foo { x: 1, y: 2 }, Foo { x: 3 }];
| ^^^ missing `y`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0063`.
struct Foo { x: i32, y: i32 }
fn main() {
let arr = [Foo { x: 1, y: 2 }, Foo { x: 3, y: 4 }];
println!("{} {}", arr[1].x, arr[1].y);
}
$ rustc --edition 2021 fix_array_of_structs.rs -o fix_array_of_structs
$ ./fix_array_of_structs
3 4
The first element provides both fields and draws no comment; the second provides x alone, and the diagnostic names that expression. An array does not average its elements - each struct expression inside it is a separate construction, checked separately, and a complete sibling does not cover for an incomplete one. The same shape inside a vec! of structs fails the same way, on the incomplete element.
The fix names y in the second element. The output is 3 4, the second element's fields read back.
The eleventh failure: two fields missing, one error
An expression that provides one field of three:
struct Foo { x: i32, y: i32, z: i32 }
fn main() {
let f = Foo { x: 1 };
println!("{} {} {}", f.x, f.y, f.z);
}
$ rustc --edition 2021 fail_missing_two.rs -o fail_missing_two
error[E0063]: missing fields `y` and `z` in initializer of `Foo`
--> fail_missing_two.rs:3:13
|
3 | let f = Foo { x: 1 };
| ^^^ missing `y` and `z`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0063`.
struct Foo { x: i32, y: i32, z: i32 }
fn main() {
let f = Foo { x: 1, y: 2, z: 3 };
println!("{} {} {}", f.x, f.y, f.z);
}
$ rustc --edition 2021 fix_missing_two.rs -o fix_missing_two
$ ./fix_missing_two
1 2 3
The header reads "missing fields y and z" - plural, in a single error, with a single span and a single label listing both names. Two missing fields are not two errors, and a repair pass that fixes the first name it sees and recompiles will find the second still listed in the same message it started from. The complete list is in the first diagnostic; read all of it before editing.
The fix names both fields. The output is 1 2 3. An empty initializer is the limiting case of this family: Foo {} on a two-field struct reports both fields missing in the same single-error form.
More shapes that are E0063
The same single error, in forms that follow the families above. Each program below fails with exactly one E0063 and no other error:
- Three of four fields missing - only the last field of a four-field struct is provided, and the other three are listed in the one error:
struct Cfg { a: i32, b: i32, c: i32, d: i32 }
fn main() {
let v = Cfg { d: 4 };
println!("{}", v.d);
}
- An empty initializer - no fields at all, and every declared field is listed as missing:
struct Foo { x: i32, y: i32 }
fn main() {
let f = Foo {};
println!("{}", f.x);
}
- A struct-like enum variant with three fields, missing the text field:
enum Msg { Data { id: i32, text: String, flag: bool } }
fn main() {
let m = Msg::Data { id: 1, flag: true };
println!("{:?}", matches!(m, Msg::Data { .. }));
}
- The incomplete element inside a
vec!rather than an array:
struct Foo { x: i32, y: i32 }
fn main() {
let v = vec![Foo { x: 1 }];
println!("{}", v.len());
}
- A missing
boolfield - the missing field's type does not change the error:
struct Flag { on: bool, count: i32 }
fn main() {
let f = Flag { count: 1 };
println!("{}", f.count);
}
- The mirror of the fourth family: the inner struct is complete and the outer expression leaves out its own field:
struct Inner { a: i32, b: i32 }
struct Outer { inner: Inner, name: String }
fn main() {
let o = Outer { inner: Inner { a: 1, b: 2 } };
println!("{}", o.inner.a);
}
- A tuple struct built with brace syntax, missing one index. Tuple structs are usually called with parentheses, and that form has a different error (it is in the traps) - but the brace form, which names fields by index as the Reference describes, is a struct expression, and an index left out of it is a missing field: the diagnostic reads "missing field
2in initializer ofColor":
struct Color(u8, u8, u8);
fn main() {
let c = Color { 0: 255, 1: 127 };
println!("{}", c.0);
}
The alternative repair: a default value as the update base
Struct update syntax does not need an existing variable as its base - any expression of the same type works, including a freshly built default:
#[derive(Default)]
struct Foo { x: i32, y: i32 }
fn main() {
let f = Foo { x: 5, ..Default::default() };
println!("{} {}", f.x, f.y);
}
$ rustc --edition 2021 ok_default_fill.rs -o ok_default_fill
$ ./ok_default_fill
5 0
#[derive(Default)]
struct Foo { x: i32, y: i32 }
fn main() {
let f = Foo { x: 7, ..Foo::default() };
println!("{} {}", f.x, f.y);
}
$ rustc --edition 2021 alt_default_expr.rs -o alt_default_expr
$ ./alt_default_expr
7 0
The first program is the seventh family's repair in its common form: ..Default::default() fills y from the derived Default, and the output is 5 0. The second builds the base explicitly - ..Foo::default() - with the same result shape, printing 7 0. The two forms differ in what they state: Default::default() asks the trait for the base without naming the type, which keeps working if the expression's type is renamed, while Foo::default() names the constructor directly. Both require the same precondition - a Default implementation - and both take every unspecified field from that one base, so a field that should hold a non-default value still has to be named explicitly, as x is in both programs.
A hand-written Default implementation serves the same fill, with its own values in place of the derived zeros:
struct Foo { x: i32, y: i32 }
impl Default for Foo { fn default() -> Foo { Foo { x: 0, y: 10 } } }
fn main() {
let f = Foo { x: 5, ..Default::default() };
println!("{} {}", f.x, f.y);
}
$ rustc --edition 2021 ok_manual_default.rs -o ok_manual_default
$ ./ok_manual_default
5 10
The output is 5 10: x from the expression, y from the hand-written implementation's 10, not a derived 0. A derived Default fills numeric fields with zero, bool fields with false, String and collection fields with empty values, and Option fields with None - the seventh family's fill - and any field whose wanted value differs from that has to be named, whichever Default form supplies the rest.
Shapes that compile as written
- Struct update syntax from a complete instance: the remaining fields come from the base, so a short field list is complete by construction. This is the eighth family's repair standing alone:
struct Foo { x: i32, y: i32 }
fn main() {
let a = Foo { x: 1, y: 2 };
let b = Foo { x: 9, ..a };
println!("{} {}", b.x, b.y);
}
$ rustc --edition 2021 ok_update_syntax.rs -o ok_update_syntax
$ ./ok_update_syntax
9 2
struct Foo { x: i32, y: i32 }
fn main() {
let x = 1;
let y = 2;
let f = Foo { x, y };
println!("{} {}", f.x, f.y);
}
$ rustc --edition 2021 ok_shorthand.rs -o ok_shorthand
$ ./ok_shorthand
1 2
The first program prints 9 2 - the named x and the y taken from the base. The second is field init shorthand: the local variables x and y have the fields' names, so writing the names alone specifies both fields, and the program prints 1 2. Shorthand specifies the field; it does not omit it - leaving a name out of a shorthand list is the second family's error again.
- Fields named out of declaration order. The Book states that fields do not have to be specified in the order they were declared, and a complete list in the other order compiles:
struct Foo { x: i32, y: i32 }
fn main() {
let f = Foo { y: 2, x: 1 };
println!("{} {}", f.x, f.y);
}
$ rustc --edition 2021 ok_out_of_order.rs -o ok_out_of_order
$ ./ok_out_of_order
1 2
The output is 1 2, the fields read in declaration order regardless of the order they were written in.
- A tuple struct built with brace syntax and every index named. The brace form is a struct expression, and a complete one compiles - it is the incomplete brace form, in the extra shapes above, that is E0063:
struct Color(u8, u8, u8);
fn main() {
let c = Color { 0: 255, 1: 127, 2: 0 };
println!("{} {} {}", c.0, c.1, c.2);
}
$ rustc --edition 2021 ok_tuple_brace_full.rs -o ok_tuple_brace_full
$ ./ok_tuple_brace_full
255 127 0
The output is 255 127 0.
Neighbors that are not E0063
- A tuple struct called with too few values is E0061, this function takes more arguments than were supplied. The parenthesized form is a call to the tuple struct's constructor, not a struct expression, so a short argument list is an arity error - the trap the brace form above avoids only by being a different syntax:
struct Point(i32, i32, i32);
fn main() {
let p = Point(0, 0);
println!("{}", p.0);
}
$ rustc --edition 2021 trap_tuple_few.rs -o trap_tuple_few
error[E0061]: this struct takes 3 arguments but 2 arguments were supplied
--> trap_tuple_few.rs:3:13
|
3 | let p = Point(0, 0);
| ^^^^^------ argument #3 of type `i32` is missing
|
note: tuple struct defined here
--> trap_tuple_few.rs:1:8
|
1 | struct Point(i32, i32, i32);
| ^^^^^
help: provide the argument
|
3 | let p = Point(0, 0, /* i32 */);
| +++++++++++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0061`.
struct Foo { x: i32, y: i32 }
fn main() {
let f = Foo { x: 0, y: 0, z: 0 };
println!("{}", f.x);
}
$ rustc --edition 2021 trap_unknown_field.rs -o trap_unknown_field
error[E0560]: struct `Foo` has no field named `z`
--> trap_unknown_field.rs:3:31
|
3 | let f = Foo { x: 0, y: 0, z: 0 };
| ^ `Foo` does not have this field
|
= note: all struct fields are already assigned
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0560`.
The repair for E0061 is to supply the missing argument in the call, in position - tuple fields have no names to fill by. The second program above is the opposite defect: every declared field is present, plus one that does not exist. An unknown field name is E0560, the struct has no field with that name - and it masks completeness: an initializer that both leaves a field out and names an unknown one reports the unknown field, and the missing field appears only after the unknown name is removed or corrected. A misspelled field name presents the same way: the intended field is reported missing only once the misspelling is fixed, because as written the expression names a field the type does not have.
- A value of the wrong type for a correctly named field is E0308, mismatched types. The field list is complete; the value is not the field's type:
struct Foo { x: i32, y: i32 }
fn main() {
let f = Foo { x: "a", y: 0 };
println!("{}", f.y);
}
$ rustc --edition 2021 trap_wrong_type.rs -o trap_wrong_type
error[E0308]: mismatched types
--> trap_wrong_type.rs:3:22
|
3 | let f = Foo { x: "a", y: 0 };
| ^^^ expected `i32`, found `&str`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0308`.
struct Foo { x: i32, y: i32 }
fn main() {
let f = Foo { x: 5, ..Default::default() };
println!("{}", f.x);
}
$ rustc --edition 2021 trap_no_default.rs -o trap_no_default
error[E0277]: the trait bound `Foo: Default` is not satisfied
--> trap_no_default.rs:3:27
|
3 | let f = Foo { x: 5, ..Default::default() };
| ^^^^^^^^^^^^^^^^^^ the trait `Default` is not implemented for `Foo`
|
help: consider annotating `Foo` with `#[derive(Default)]`
|
1 + #[derive(Default)]
2 | struct Foo { x: i32, y: i32 }
|
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0277`.
The repair is a value of the field's type, not a field-list change. The second program is the Default fill attempted on a type with no Default implementation: the trait requirement fails as E0277 before any field accounting, because ..Default::default() asks for a base the type cannot provide. The repairs are to derive or implement Default - the manual implementation above is one - or to name every field.
- A field named twice is E0062, the field is specified more than once. The index's rule is "exactly once": too many specifications fail just as too few do, with the span on the repeated name:
struct Foo { x: i32, y: i32 }
fn main() {
let f = Foo { x: 0, x: 1, y: 2 };
println!("{}", f.x);
}
$ rustc --edition 2021 trap_duplicate_field.rs -o trap_duplicate_field
error[E0062]: field `x` specified more than once
--> trap_duplicate_field.rs:3:25
|
3 | let f = Foo { x: 0, x: 1, y: 2 };
| ---- ^ used more than once
| |
| first use of `x`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0062`.
struct Foo { x: i32, y: i32 }
fn main() {
let f = Foo { x: 1, y: 2 };
let Foo { x } = f;
println!("{}", x);
}
$ rustc --edition 2021 trap_pattern_missing.rs -o trap_pattern_missing
error[E0027]: pattern does not mention field `y`
--> trap_pattern_missing.rs:4:9
|
4 | let Foo { x } = f;
| ^^^^^^^^^ missing field `y`
|
help: include the missing field in the pattern
|
4 | let Foo { x, y } = f;
| +++
help: if you don't care about this missing field, you can explicitly ignore it
|
4 | let Foo { x, y: _ } = f;
| ++++++
help: or always ignore missing fields here
|
4 | let Foo { x, .. } = f;
| ++++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0027`.
The repair is to delete the repeated entry. The second program is the pattern-side relative: a struct pattern that binds one field without accounting for the rest is E0027, the pattern does not mention a field - patterns have their own completeness rule and their own code, and the pattern repair is to add the missing binding or a .. rest pattern, which has no equivalent in an initializer expression.
- A private field named from outside its module is E0451, the field is private. The field exists and the list would be complete, but the naming itself is not permitted from here - the Reference's rule that all of a struct's fields must be visible to the expression, even the ones taken from a base, is the same visibility line:
mod m {
pub struct S { pub a: i32, b: i32 }
impl S {
pub fn b(&self) -> i32 { self.b }
}
}
fn main() {
let s = m::S { a: 1, b: 2 };
println!("{} {}", s.a, s.b());
}
$ rustc --edition 2021 trap_private_field.rs -o trap_private_field
error[E0451]: field `b` of struct `S` is private
--> trap_private_field.rs:8:26
|
8 | let s = m::S { a: 1, b: 2 };
| ^ private field
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0451`.
The repair is to build the value inside the module - a constructor the module provides - or to make the field public. Note the asymmetry with the sixth family: a missing public field is E0063, while a present-but-private field is E0451, and an expression that leaves a private field out entirely still cannot use update syntax from outside the module to cover it, because the covered fields must be visible too.
Checklist
- The header checked: E0063 means a struct expression left out at least one declared field - if the code is E0061, E0560, E0308, E0277, E0062, E0027, or E0451, the neighbors name the difference before any edit.
- The whole missing list read before editing: several missing fields arrive in one error, and fixing only the first name in the header leaves the rest for the next compile.
- The source of each missing value decided: a value the program computes belongs in the expression by name; a value that should be the type's default belongs to a
..Default::default()fill; a value that should match an existing instance belongs to struct update syntax. - For a Default fill, the implementation checked: the type must derive or implement
Default, and every filled field's default must be the value actually wanted - a derived default is zero, empty,false, orNone, field by field. - For update syntax, the base checked: same type, and its unspecified fields are moved or copied from it - after the expression, the base's non-
Copyfields that were taken are no longer available on the base. - Unknown names resolved first: an initializer that names a field the type does not have reports E0560 and hides its missing fields until the name is corrected.
- The fixed program run, and its output checked against what the program was meant to print - a fill that compiles with a default where a computed value belonged is a wrong program that passes the compiler.
Related guides on this site
- E0164 Expected Tuple Struct or Variant Fix
- E0060 Variadic Function Missing Argument Fix
- E0026 Struct Variant Has No Field Fix
- Struct Literal on Non-Struct Type E0071 Fix
Sources
- Rust, Error codes index: E0063 — for the definition of the error as a field of a struct or struct-like enum variant not provided, for the two-field example missing one field, and for each field being specified exactly once, with the completed initializer as the repair.
- The Rust Programming Language, Defining and Instantiating Structs — for creating an instance by naming the struct and giving key-value pairs for its fields, for fields not needing to be specified in declaration order, for field init shorthand, and for struct update syntax taking the remaining fields from another instance.
- The Rust Reference, Struct expressions — for a struct expression as a path followed by the values for the fields, for the functional update with
..taking the remaining fields from a base of the same type, for all fields needing to be visible to the expression, and for field init shorthand and tuple-struct fields named by index.
Keep Exploring
The full Rust hub \u2014 ownership, structs and enums, async, and measured comparisons.
Every guide on the site in one list, grouped by topic.
Every browser-only checker and verifier on the site.
The C++, Python, Sysadmin, Rust, Go, and TypeScript hubs.