Rust
E0451 Private Struct Field Fix
E0451 is the construction error: a struct literal in one module named a field that the defining module kept private. The struct itself can be public, the literal can name every field correctly, and the values can all be right - the literal still fails, because a struct expression may only be written where every field it touches is visible. Leaving the private field out does not help, and neither does functional update syntax: the update tail still has to move the private field, so it fails with the same code. The repairs are to widen the field, or to stop writing the literal outside the defining module and call a constructor the module provides instead. Two nearby mistakes carry different codes - touching a private field on a value you already have is E0616, and constructing a tuple struct with a private field is E0603 - and this guide separates them from E0451 shape by shape.
Written November 30, 2026 · Tested with Rust 1.99.0 · 21 min read
- The rule underneath: field visibility follows the module
- How to read the diagnostic
- The first failures: naming a private field in a literal
- Leaving the private field out does not help
Written November 30, 2026
Tested with Rust 1.99.0
What this guide is
Error E0451 is about who may write a struct literal. The error codes index opens: "A struct constructor with private fields was invoked." A field with no pub is private to the module that defines the struct, so a literal written in any other module cannot name it - and, as the failures below show, cannot leave it to an update tail either. The index's own example is a struct with one public field and one private field, constructed from outside the defining module, and it fails on the private field alone.
The practical payoff is the split from the two neighbors. E0616 is about access to a field of a value that already exists: reading or writing value.field from outside the defining module. E0451 is about construction: building the value with a literal in the first place. A tuple struct draws a third line: it has no field names to put in a literal, and in the probed set a tuple struct with a private field fails as E0603, because its constructor itself is private. Which code you get depends on the shape of the expression, not on which field is private.
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: field visibility follows the module
The Rust Reference's visibility and privacy page states the default: "By default, everything is private", with two exceptions - associated items in a public trait, and enum variants in a public enum, are public by default. A struct field follows that default: declared without pub, it is visible in the module that defines the struct and in that module's descendants, and nowhere else. Declaring the struct itself pub publishes the type, not its fields; each field needs its own visibility.
A struct literal is checked against that visibility. The Reference's struct expressions page states the constraint for the functional update form, and phrases it as a rule for struct expressions generally: "As with all struct expressions, all of the fields of the struct must be visible, even those not explicitly named." That one sentence predicts most of this guide: naming a private field fails, omitting it fails differently, and an update tail that would move it fails the same way as naming it, because the fields the tail supplies still have to be visible where the literal is written.
How to read the diagnostic
In the probed set, an E0451 failure is a single error whose header names the private fields: "field b of struct Foo is private" for one field, "fields x and y of struct Point are private" for two. The span underlines each private field name in the literal - or, for an update tail, the whole .. expression, labeled with the private field it would have to move. There are no notes and no help text in the clean cases below; the header and the underlines together are the whole diagnosis: the named fields are the ones not visible where the literal stands.
One shape in this guide breaks that pattern, and it is the shape most people try first. A literal that names only the public fields and simply leaves the private field out does not produce E0451 at all - it produces an uncoded error, shown in its own section below. When the diagnostic has no code, do not assume it is a different problem; here it is the same private field, reported from the other side.
The first failures: naming a private field in a literal
The smallest shape is a struct whose fields are all private, defined in one module and constructed in another. The struct is public, a method on it reads both fields, and the literal in main names both fields with values of the right types:
mod inner {
pub struct Point { x: i32, y: i32 }
impl Point {
pub fn sum(&self) -> i32 { self.x + self.y }
}
}
fn main() {
let p = inner::Point { x: 1, y: 2 };
println!("{}", p.sum());
}
error[E0451]: fields `x` and `y` of struct `Point` are private
--> lit.rs:8:28
|
8 | let p = inner::Point { x: 1, y: 2 };
| ^ ^ private field
| |
| private field
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0451`.
Both fields are underlined, and the header names both. Nothing about the values is wrong; the literal is simply written in a module where neither field is visible. Making one field public narrows the error to the other field - the error index's own example shape, a public field and a private field in the same literal:
mod bar {
pub struct Foo { pub a: isize, b: isize }
impl Foo {
pub fn total(&self) -> isize { self.a + self.b }
}
}
fn main() {
let f = bar::Foo { a: 0, b: 0 };
println!("{}", f.total());
}
error[E0451]: field `b` of struct `Foo` is private
--> mix.rs:8:30
|
8 | let f = bar::Foo { a: 0, b: 0 };
| ^ private field
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0451`.
Only the private field is named and underlined. The public field in the same literal draws no complaint, which is the first hint that the check is per field: a literal may name any field that is visible where it is written, and E0451 reports exactly the fields that are not.
Leaving the private field out does not help
The natural workaround is to set the public fields and leave the private one unset. That literal fails too, with a different diagnostic - no error code, a span over the whole literal, and a note naming the private field that was not provided:
mod bar {
pub struct Foo { pub a: isize, b: isize }
impl Foo {
pub fn total(&self) -> isize { self.a + self.b }
}
}
fn main() {
let f = bar::Foo { a: 0 };
println!("{}", f.total());
}
error: cannot construct `Foo` with struct literal syntax due to private fields
--> omit.rs:8:13
|
8 | let f = bar::Foo { a: 0 };
| ^^^^^^^^
|
= note: ...and other private field `b` that was not provided
error: aborting due to 1 previous error
Read the note carefully: "...and other private field b that was not provided". The problem is not that a field is missing in the abstract - leaving out a public field is E0063, a coded missing-field error - it is that this literal cannot provide the field, because the field is private here, and a struct literal must account for every field. There is no partial literal for a struct with private fields, written from outside the defining module.
Functional update syntax does not route around it
The second workaround is functional update syntax: name the fields you may name, and take the rest from a base value the defining module hands you. The base here is built by a constructor function inside the defining module, so the base value itself is legitimate - and the literal still fails:
mod inner {
pub struct Config { pub name: String, retries: u32 }
impl Config {
pub fn base() -> Config { Config { name: String::from("a"), retries: 3 } }
pub fn describe(&self) -> String { format!("{}:{}", self.name, self.retries) }
}
}
fn main() {
let c = inner::Config { name: String::from("b"), ..inner::Config::base() };
println!("{}", c.describe());
}
error[E0451]: field `retries` of struct `Config` is private
--> fru.rs:9:56
|
9 | let c = inner::Config { name: String::from("b"), ..inner::Config::base() };
| ^^^^^^^^^^^^^^^^^^^^^ field `retries` is private
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0451`.
The span moves to the .. tail and the label names the private field it would move. Naming no fields at all changes nothing - a literal that is only an update tail fails the same way:
mod inner {
pub struct Config { pub name: String, retries: u32 }
impl Config {
pub fn base() -> Config { Config { name: String::from("a"), retries: 3 } }
pub fn describe(&self) -> String { format!("{}:{}", self.name, self.retries) }
}
}
fn main() {
let c = inner::Config { ..inner::Config::base() };
println!("{}", c.describe());
}
error[E0451]: field `retries` of struct `Config` is private
--> fruonly.rs:9:31
|
9 | let c = inner::Config { ..inner::Config::base() };
| ^^^^^^^^^^^^^^^^^^^^^ field `retries` is private
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0451`.
And the tail's source does not matter. A derived Default implementation is a common base for this form, and ..Default::default() as the tail fails identically:
mod inner {
#[derive(Default)]
pub struct Config { pub name: String, retries: u32 }
impl Config {
pub fn describe(&self) -> String { format!("{}:{}", self.name, self.retries) }
}
}
fn main() {
let c = inner::Config { name: String::from("x"), ..Default::default() };
println!("{}", c.describe());
}
error[E0451]: field `retries` of struct `Config` is private
--> deffru.rs:9:56
|
9 | let c = inner::Config { name: String::from("x"), ..Default::default() };
| ^^^^^^^^^^^^^^^^^^ field `retries` is private
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0451`.
All three are the Reference's sentence at work: the fields supplied by the tail must be visible where the literal is written, even though the literal never names them. Functional update syntax is a way to avoid repeating values, not a way to move private fields across a module boundary. (Default does repair this error - as a whole value, not as a tail. The fixes section shows that form.)
Destructuring names fields too
Construction is not the only place a literal-like form names fields. A pattern that destructures a value and binds a private field by name is also E0451 in the probed set - here the value itself comes from a function in the defining module, so it exists legitimately, and only the pattern reaches for the private field:
mod inner {
pub struct Point { pub x: i32, y: i32 }
pub fn make() -> Point { Point { x: 1, y: 2 } }
}
fn main() {
let inner::Point { x, y } = inner::make();
println!("{} {}", x, y);
}
error[E0451]: field `y` of struct `Point` is private
--> destr.rs:6:27
|
6 | let inner::Point { x, y } = inner::make();
| ^ private field
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0451`.
The span underlines the private binding, and the header matches the construction form. The boundary is the name, not the direction of the data flow: writing the field in a literal and binding it in a pattern both name it outside its module. What a pattern may do instead is skip the private field with .., as the controls show.
What is allowed: the controls
The same literal that fails in main compiles inside the defining module, where the fields are visible:
mod inner {
pub struct Point { x: i32, y: i32 }
pub fn run() {
let p = Point { x: 1, y: 2 };
println!("{} {}", p.x, p.y);
}
}
fn main() { inner::run(); }
1 2
The output is 1 2, the two field values. Visibility extends one step further, to descendant modules: a child module of the defining module can also write the literal and read the fields:
mod inner {
pub struct Point { x: i32, y: i32 }
pub mod child {
use super::Point;
pub fn make() -> Point { Point { x: 1, y: 2 } }
pub fn sum(p: &Point) -> i32 { p.x + p.y }
}
}
fn main() {
let p = inner::child::make();
println!("{}", inner::child::sum(&p));
}
3
The output is 3, the sum the child module computes. The boundary is the defining module's subtree, not the file and not the crate: main, outside that subtree, is where every failure in this guide stands.
Widening the field to the crate works from anywhere in the same crate. With both fields declared pub(crate), the literal in main - a different module of the same crate - compiles and runs:
mod inner {
pub struct Point { pub(crate) x: i32, pub(crate) y: i32 }
}
fn main() {
let p = inner::Point { x: 3, y: 4 };
println!("{} {}", p.x, p.y);
}
3 4
The output is 3 4. This control is scoped to one crate: every program in this guide is a single crate, and nothing here speaks to what pub(crate) does across a crate boundary, where by definition it grants nothing.
Destructuring with .. skips the private field instead of naming it, and compiles and runs - the same value and the same defining module as the destructuring failure, with only the public field bound:
mod inner {
pub struct Point { pub x: i32, y: i32 }
pub fn make() -> Point { Point { x: 1, y: 2 } }
}
fn main() {
let inner::Point { x, .. } = inner::make();
println!("{}", x);
}
1
The output is 1, the public field's value. The .. in a pattern discards the rest of the value without naming its fields, which is why this form is allowed where the update tail in a literal is not: the pattern's .. moves nothing by name into a new value.
Two shapes have no private field to trip on. A unit struct and an empty-braces struct, both defined in another module, construct from outside without error:
mod inner {
pub struct Marker;
pub struct Empty {}
}
fn main() {
let _m = inner::Marker;
let _e = inner::Empty {};
println!("ok");
}
ok
The output is ok. And an enum's struct variant is not a struct for this rule: variant fields carry no visibility of their own, enum variants in a public enum are public by default per the Reference, and constructing the variant from outside the defining module compiles and runs:
mod inner {
pub enum Shape { Rect { w: u32, h: u32 }, Unit }
impl Shape {
pub fn area(&self) -> u32 {
match self {
Shape::Rect { w, h } => w * h,
Shape::Unit => 0,
}
}
}
}
fn main() {
let s = inner::Shape::Rect { w: 3, h: 4 };
println!("{}", s.area());
}
12
The output is 12, the area the method computes. If a "private field" error points at what looks like a variant, check whether the type is actually a struct - in the probed set, the variant form does not produce E0451.
The traps: neighbors that are not E0451
Touching a private field on a value you already have is E0616, not E0451. The value here is built inside the defining module and returned; reading its private field from main fails as field access:
mod inner {
pub struct Point { pub x: i32, y: i32 }
pub fn make() -> Point { Point { x: 1, y: 2 } }
}
fn main() {
let p = inner::make();
println!("{}", p.y);
}
error[E0616]: field `y` of struct `Point` is private
--> read.rs:7:22
|
7 | println!("{}", p.y);
| ^ private field
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0616`.
Writing the field fails the same way, with the same code and the same header - in the probed set, reads and writes through dot access are one error:
mod inner {
pub struct Point { pub x: i32, y: i32 }
pub fn make() -> Point { Point { x: 1, y: 2 } }
}
fn main() {
let mut p = inner::make();
p.y = 9;
println!("{}", p.x);
}
error[E0616]: field `y` of struct `Point` is private
--> write.rs:7:7
|
7 | p.y = 9;
| ^ private field
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0616`.
The dividing line is construction versus access. If the expression builds the value - a literal, an update tail, a pattern that binds the field - a private field is E0451. If the value already exists and the expression reaches into it with dot access, a private field is E0616, and the E0616 guide works that side of the line.
A tuple struct has no field names, so there is no literal for E0451 to reject. Instead, the tuple struct's constructor is itself private when any of its fields is private - one public field out of two does not save it:
mod inner {
pub struct Pair(pub u8, u16);
}
fn main() {
let p = inner::Pair(1, 2);
println!("{}", p.0);
}
error[E0603]: tuple struct constructor `Pair` is private
--> tuple.rs:5:20
|
2 | pub struct Pair(pub u8, u16);
| ----------- a constructor is private if any of the fields is private
...
5 | let p = inner::Pair(1, 2);
| ^^^^ private tuple struct constructor
|
note: the tuple struct constructor `Pair` is defined here
--> tuple.rs:2:5
|
2 | pub struct Pair(pub u8, u16);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: consider making the fields publicly accessible
|
2 | pub struct Pair(pub u8, pub u16);
| +++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0603`.
The code is E0603, the private-item code, and the diagnostic says why in its own note on the definition: "a constructor is private if any of the fields is private". Its help performs the repair - make the remaining field public - which is also the first fix below, reached from a different code.
Fixes in reach order
First, when outside code is meant to build the value directly, make the private field public. The mixed struct from the failures, with the private field widened, constructs from main and runs:
mod bar {
pub struct Foo { pub a: isize, pub b: isize }
}
fn main() {
let f = bar::Foo { a: 0, b: 5 };
println!("{} {}", f.a, f.b);
}
0 5
The output is 0 5. This is the error index's first repair - "ensure that all the fields of the struct are public" - and the tuple trap's help suggests the same move. Widen only the fields outside code genuinely needs; a field widened to pub(crate), as in the controls, is enough when the constructing code lives in the same crate.
Second, when the field should stay private, keep the literal inside the defining module and export a constructor function - the error index's second repair. The literal moves into an associated function, and outside code calls it:
mod bar {
pub struct Foo { pub a: isize, b: isize }
impl Foo {
pub fn new() -> Foo { Foo { a: 0, b: 0 } }
pub fn b(&self) -> isize { self.b }
}
}
fn main() {
let f = bar::Foo::new();
println!("{} {}", f.a, f.b());
}
0 0
The output is 0 0, the field values the constructor sets, with the private one read back through a method the module also provides. This is the repair that preserves the privacy the field had: callers get values the module chose, not write access to the field.
Third, when the type implements Default, build the whole value through it instead of writing a literal with a Default tail. The tail form failed above; the whole-value form compiles and runs, because no struct expression is written outside the defining module at all:
mod inner {
#[derive(Default)]
pub struct Config { pub name: String, retries: u32 }
impl Config {
pub fn retries(&self) -> u32 { self.retries }
}
}
fn main() {
let c = inner::Config::default();
println!("{} {}", c.name.len(), c.retries());
}
0 0
The output is 0 0 - the length of the default string and the default value of the private field, read through its accessor. The contrast with the tail failure is the guide in miniature: Default::default() as a function call crosses the module boundary freely; the same value used as the tail of a literal does not, because the literal is still a struct expression naming the struct's fields by implication.
Last, match the repair to the code. An E0616 diagnostic is not repaired by any of the three fixes above as stated - it asks for the field to be widened or for an accessor method, on a value that already exists. An E0603 diagnostic on a tuple struct is repaired by widening the private field or by a constructor function, the same two moves, but the code to search by is the private-item code.
Decision summary
E0451, in this guide's probed set, fired exactly when code outside a struct's defining module named - or had to account for - a private field while building or destructuring a value: a literal naming all fields (all-private and mixed structs), a functional-update literal naming only public fields, an update-only literal, a literal with a ..Default::default() tail, and a destructuring pattern binding the private field. It did not fire as E0451 in three neighboring shapes: a literal that omitted the private field failed with an uncoded "cannot construct ... due to private fields" error; reading or writing a private field on an existing value was E0616; and constructing a tuple struct with a private field was E0603, because the constructor itself is private. It did not fire at all inside the defining module or its descendant modules, for pub(crate) fields constructed elsewhere in the same crate, for unit and empty structs, for an enum's struct variant, or for a destructuring pattern that skipped the private field with ... The repairs act on visibility or on where the literal is written: widen the field, export a constructor function from the defining module, or build the whole value through Default - never through a literal tail.
Checklist
- Read the header's fields first: in the probed set, E0451 names exactly the fields that are not visible where the literal or pattern stands, and underlines each one - or the
..tail that would move it. - Do not try to omit your way out: in the probed set, a literal naming only the public fields failed with an uncoded "cannot construct ... due to private fields" error, and an update tail - from a base value or from
Default::default()- was still E0451. - Separate construction from access before choosing a repair: building or destructuring with a private field is E0451 in the probed set; reading or writing the field on an existing value is E0616, and a tuple struct with a private field is E0603.
- Widen deliberately:
pubwhen any crate may construct the value,pub(crate)when the constructing code is in the same crate - in the probed set, both made the literal compile, and thepub(crate)control is a single-crate result. - Keep the field private when it should stay private: move the literal into the defining module behind a constructor function, or build the whole value with
Default::default()- a function call, not a literal tail. - Remember the free shapes: in the probed set, literals inside the defining module and its descendants, unit and empty structs, enum struct variants, and patterns that skip private fields with
..needed no repair at all.
Related guides on this site
- E0026 Struct Variant Has No Field Fix
- E0560 Struct Has No Field Named Fix
- E0616 Field Is Private Fix
- Fix Rust E0597 "`x` does not live long enough" — a Block Scope, a Stored Reference, a Struct Field, and a Closure Capture
Sources
- Rust error codes index, E0451 - a struct constructor with private fields was invoked; the erroneous example
bar::Foo{ a: 0, b: 0 }against a struct with a public fieldaand a private fieldb, and the two repairs: ensure that all the fields of the struct are public, or implement a function for easy instantiation, shown as aFoo::new()constructor that writes the literal inside the defining module. - Rust error codes index, E0616 - the neighboring access code: an attempt to access a private field on a struct; its erroneous example reads a private field on a value built by a constructor, and its options are to set the field public or to add a getter function.
- Rust error codes index, E0603 - the neighboring private-item code: a private item was used outside its scope, repaired by making the item public with the
pubkeyword; in this guide it is the code a tuple struct constructor carries when any of the struct's fields is private. - The Rust Reference, Visibility and privacy - by default, everything is private, with two exceptions: associated items in a public trait are public by default, and enum variants in a public enum are also public by default; an item declared
pubcan be accessed from outside its module. - The Rust Reference, Struct expressions - a struct expression with functional update syntax takes the fields that were specified from the literal and moves or copies the remaining fields from the base expression, and, as with all struct expressions, all of the fields of the struct must be visible, even those not explicitly named.
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.