Rust

E0616 Field Is Private Fix

A struct's fields belong to the module that defines the struct, not to the struct's name - a pub struct can still keep every field private. E0616 is the compiler saying a field access crossed a module boundary the field's visibility does not reach: a read, a write, a read one hop in through another struct, a read from the parent module, a read from a sibling module. This guide separates the five shapes that produce it and matches each one to its repair: reach the field through a method, widen the field's visibility to exactly the readers it has, or move the access into the module that owns the field.

Written November 24, 2026 · Tested with Rust 1.99.1 · 23 min read

By the end of this guide
  • The rule underneath: a private field is visible in its module and that module's descendants
  • How to read the diagnostic
  • The first failure: reading a private field from outside the module
  • The second failure: assigning to a private field from outside the module

Written November 24, 2026

Tested with Rust 1.99.1

What this guide is

Error E0616 is a field access that crosses a module boundary the field is not visible across. The error codes index defines it in one sentence: an attempt was made to access a private field on a struct, and its example is a pub struct Foo in a module whose x field stays private, read from outside the module. The header names both parties - "field value of struct Meter is private" - and that pair is the whole diagnosis: the field, the struct that owns it, and an access written somewhere the field does not reach.

The forms in this guide each reproduce E0616 with a small, complete program, and each puts the field access on a different side of a different boundary. A read from the crate root. A write from the crate root. A read one hop in, through a public field of an outer struct, where the first hop is legal and the second is not. A read from the parent of the module that defines the struct. And a read from a sibling module. The fixes fall into two shapes: reach the field through a method defined where the field is visible - an accessor, a setter, a forwarding method on the outer struct - or widen the field's visibility to exactly the readers it has, with pub or with the restricted pub(crate) form.

Several neighboring results are not the plain E0616 field-access shape and are worked only in the traps and the checklist. Field access from inside the defining module compiles, and so does access from a descendant of that module - privacy runs downward. A tuple struct's numbered field is a field like any other, and reading a private one is E0616 itself. Building the struct with a literal outside its module is not E0616: it is E0451. Calling a private method is not E0616 either: it is E0624. And naming a struct that is itself private is E0603, which fails before any field is reached.

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: a private field is visible in its module and that module's descendants

The Rust Reference's visibility and privacy page states the default first: by default, everything is private. Marking the struct pub changes the visibility of the struct only; each field carries its own visibility, and an unmarked field stays private. The Reference then gives the two access cases. A public item can be accessed from outside its module. A private item may be accessed by the current module and its descendants - the module that defines it, and any module nested inside that one, at any depth. Every other position is outside the field's reach: the parent of the defining module, a sibling module, the crate root, and another crate all stand on the far side of the boundary, which is why the five families below fail from five different directions for one reason.

The same page defines the restricted forms of pub that two of the fixes use. pub(crate) makes an item visible within the current crate. pub(super) makes an item visible to the parent module. These forms widen a field's visibility to a named scope instead of to everyone, so a field whose readers are all inside the crate, or all in one parent module, never has to become fully public to become reachable.

The error codes index names the two repairs it considers canonical: set the field public, or add a getter function. The Rust Book's encapsulation discussion explains what the choice between them costs. In its AveragedCollection example, the struct is marked pub while its fields remain private, and the public methods are the only ways to access or modify the data - which is what lets the internals change later without the callers changing. A pub field is the opposite commitment: every reader and writer of the field is now part of the type's surface, and the representation cannot change underneath them.

How to read the diagnostic

Every failure in the five families prints the same shape of output. The first family's, in full:

Terminal
$ rustc --edition 2021 fail_read.rs -o fail_read
error[E0616]: field `value` of struct `Meter` is private
  --> fail_read.rs:15:22
   |
15 |     println!("{}", m.value);
   |                      ^^^^^ private field

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0616`.

Read it in order. The header names the field first and the struct that owns it second - value, of Meter. The caret span underlines just the field name at the access site, and its note is the header restated at the span: "private field". There is no secondary span pointing back at the field's definition, because the definition is not in dispute: the field exists, its type is known, and the one fact that decides the error is the position of the access relative to the module that defines the struct. The repair is chosen by asking who should be able to reach the field. If no new reader should, the access moves behind a method defined inside the module. If a known set of modules should, the field's visibility widens to exactly that set. If the access is simply in the wrong place, it moves into the defining module or one of its descendants, where it was legal all along.

That single-span shape is also the fastest way to tell this error from its neighbors. When the failing name is a method rather than a field, the header says so and a second span points at the method's definition - E0624, in the traps. When the struct itself is private, the failure is at the path that names the type, before any field is mentioned - E0603. And when the value is being built rather than read - a struct literal, a destructuring pattern - the header names the same privacy fact under a different code, E0451.

The first failure: reading a private field from outside the module

A program that reads a field from the crate root, where the struct is defined in a module:

Rust
mod meter {
    pub struct Meter {
        value: i32,
    }

    impl Meter {
        pub fn new(value: i32) -> Meter {
            Meter { value }
        }
    }
}

fn main() {
    let m = meter::Meter::new(7);
    println!("{}", m.value);
}
Terminal
$ rustc --edition 2021 fail_read.rs -o fail_read
error[E0616]: field `value` of struct `Meter` is private
  --> fail_read.rs:15:22
   |
15 |     println!("{}", m.value);
   |                      ^^^^^ private field

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0616`.

Everything else in this program is public: the struct is pub, the constructor is pub, and main holds a Meter it obtained legally. Only the field is private, because a field's visibility is its own - pub on the struct does not cascade into it. The shape appears wherever a type is written to be constructed through a function and then read as if it were a plain record: the construction was moved behind an API and the field reads were left behind.

The fix adds an accessor method, defined inside the module where the field is visible:

Rust
mod meter {
    pub struct Meter {
        value: i32,
    }

    impl Meter {
        pub fn new(value: i32) -> Meter {
            Meter { value }
        }

        pub fn value(&self) -> i32 {
            self.value
        }
    }
}

fn main() {
    let m = meter::Meter::new(7);
    println!("{}", m.value());
}
Terminal
$ rustc --edition 2021 fix_read.rs -o fix_read
$ ./fix_read
7

The output is 7. The method body sits inside mod meter, where self.value is an ordinary access; the caller reaches the same number through the method instead of the field. This is the error index's second option, and it is the repair that keeps the field private: the stored representation can change - a different field, a computed value - without any caller changing, which is the property the Book's encapsulation example is built on. The index's first option, marking the field pub, is the fourth family's fix, and it belongs to fields that are plain data with no invariant to protect.

The second failure: assigning to a private field from outside the module

A program that writes to a field from the crate root:

Rust
mod counter {
    pub struct Counter {
        count: u32,
    }

    impl Counter {
        pub fn new() -> Counter {
            Counter { count: 0 }
        }

        pub fn count(&self) -> u32 {
            self.count
        }
    }
}

fn main() {
    let mut c = counter::Counter::new();
    c.count = 5;
    println!("{}", c.count());
}
Terminal
$ rustc --edition 2021 fail_write.rs -o fail_write
error[E0616]: field `count` of struct `Counter` is private
  --> fail_write.rs:19:7
   |
19 |     c.count = 5;
   |       ^^^^^ private field

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0616`.

An assignment is a field access with the field on the left-hand side, and it is checked by the same rule. This program already has a read accessor - count() compiles and is used in the last line - and it does not help, because a getter grants reading, not writing. The span sits under the field name in the assignment, at the start of the line. The shape appears when a counter, a total, or a state field is meant to be adjusted by callers directly, in code written as if the field were a public variable.

The fix adds a setter method alongside the getter:

Rust
mod counter {
    pub struct Counter {
        count: u32,
    }

    impl Counter {
        pub fn new() -> Counter {
            Counter { count: 0 }
        }

        pub fn count(&self) -> u32 {
            self.count
        }

        pub fn set_count(&mut self, count: u32) {
            self.count = count;
        }
    }
}

fn main() {
    let mut c = counter::Counter::new();
    c.set_count(5);
    println!("{}", c.count());
}
Terminal
$ rustc --edition 2021 fix_write.rs -o fix_write
$ ./fix_write
5

The output is 5, read back through the getter after the setter runs. The write now passes through a method that takes &mut self, so the caller still needs a mutable binding - let mut c stays - but the field itself is touched only inside the module. A setter is also the place a constraint on the value would live: a range check or a clamp inside set_count governs every write, where the same check beside a public field governs only the writes someone remembered to guard.

The third failure: reading a private field one hop in

A program that reaches a private field through a public field of an outer struct:

Rust
mod meter {
    pub struct Inner {
        value: i32,
    }

    pub struct Outer {
        pub inner: Inner,
    }

    pub fn make() -> Outer {
        Outer {
            inner: Inner { value: 7 },
        }
    }
}

fn main() {
    let o = meter::make();
    println!("{}", o.inner.value);
}
Terminal
$ rustc --edition 2021 fail_nested.rs -o fail_nested
error[E0616]: field `value` of struct `Inner` is private
  --> fail_nested.rs:19:28
   |
19 |     println!("{}", o.inner.value);
   |                            ^^^^^ private field

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0616`.

The access is a chain, o.inner.value, and each hop is checked on its own. The first hop is legal: inner is a pub field of Outer, so code outside the module may name it. The second hop crosses the boundary: value is a private field of Inner. The header is the tell - it names Inner, the struct that owns the failing field, not Outer, the struct the expression started from. The shape appears when a wrapper type exposes a component publicly and callers keep going one level deeper than the wrapper's author intended them to reach.

The fix adds a forwarding method to Outer, defined inside the module:

Rust
mod meter {
    pub struct Inner {
        value: i32,
    }

    pub struct Outer {
        pub inner: Inner,
    }

    impl Outer {
        pub fn value(&self) -> i32 {
            self.inner.value
        }
    }

    pub fn make() -> Outer {
        Outer {
            inner: Inner { value: 7 },
        }
    }
}

fn main() {
    let o = meter::make();
    println!("{}", o.value());
}
Terminal
$ rustc --edition 2021 fix_nested.rs -o fix_nested
$ ./fix_nested
7

The output is 7. Inside mod meter, self.inner.value is a legal chain - both hops are at home there - so the method can read the deep field and hand the value out at the wrapper's own level. The caller now stops at the type it actually holds. The alternative repair, marking Inner's field pub, also compiles, but it publishes the inner type's internals to every holder of an Outer; the forwarding method publishes one number and keeps the inner field private.

The fourth failure: reading a private field from the parent module

A program that reads a field from the module that contains the defining module:

Rust
mod outer {
    pub mod inner {
        pub struct Meter {
            value: i32,
        }

        impl Meter {
            pub fn new(value: i32) -> Meter {
                Meter { value }
            }
        }
    }

    pub fn read() {
        let m = inner::Meter::new(7);
        println!("{}", m.value);
    }
}

fn main() {
    outer::read();
}
Terminal
$ rustc --edition 2021 fail_parent.rs -o fail_parent
error[E0616]: field `value` of struct `Meter` is private
  --> fail_parent.rs:16:26
   |
16 |         println!("{}", m.value);
   |                          ^^^^^ private field

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0616`.

This is the family that surprises, because the accessing code in outer wraps the defining module - it can name inner, name Meter, and call the constructor. Privacy does not run that way. A private item is accessible from its own module and from that module's descendants; outer is inner's ancestor, not its descendant, so the access stands outside the field's reach exactly as a stranger's would. The direction is one-way: code inside inner could read private items of outer, and the traps show the descendant case compiling, but a parent gets no reciprocal right over a child's fields.

The fix marks the field pub:

Rust
mod outer {
    pub mod inner {
        pub struct Meter {
            pub value: i32,
        }

        impl Meter {
            pub fn new(value: i32) -> Meter {
                Meter { value }
            }
        }
    }

    pub fn read() {
        let m = inner::Meter::new(7);
        println!("{}", m.value);
    }
}

fn main() {
    outer::read();
}
Terminal
$ rustc --edition 2021 fix_parent.rs -o fix_parent
$ ./fix_parent
7

The output is 7. This is the error index's first option, and it fits a field that is plain data - a reading with no invariant, where an accessor would only repeat the field's name as a method. One narrower form is worth knowing before reaching for pub: pub(super) makes an item visible to the parent module only, so pub(super) value repairs this exact access while keeping the field private to the rest of the crate. pub is the right width when readers beyond the parent exist or are expected; pub(super) is the right width when the parent is the only outside reader there will be.

The fifth failure: reading a private field from a sibling module

A program that reads a field from a module beside the defining module:

Rust
mod inner {
    pub struct Meter {
        value: i32,
    }

    impl Meter {
        pub fn new(value: i32) -> Meter {
            Meter { value }
        }
    }
}

mod reader {
    pub fn read() {
        let m = crate::inner::Meter::new(7);
        println!("{}", m.value);
    }
}

fn main() {
    reader::read();
}
Terminal
$ rustc --edition 2021 fail_sibling.rs -o fail_sibling
error[E0616]: field `value` of struct `Meter` is private
  --> fail_sibling.rs:16:26
   |
16 |         println!("{}", m.value);
   |                          ^^^^^ private field

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0616`.

inner and reader are both children of the crate root, side by side. Neither is a descendant of the other, so reader is outside the field's reach - the same position the crate root itself is in, in the first family. The path in the access, crate::inner::Meter, resolves fine: the module and the struct are visible, the constructor is callable, and only the last step, naming the field, fails. The shape appears when one part of a crate is split into cooperating modules - a model module and a reporting module, say - and the cooperation quietly assumes the modules share their internals.

The fix widens the field to the crate with pub(crate):

Rust
mod inner {
    pub struct Meter {
        pub(crate) value: i32,
    }

    impl Meter {
        pub fn new(value: i32) -> Meter {
            Meter { value }
        }
    }
}

mod reader {
    pub fn read() {
        let m = crate::inner::Meter::new(7);
        println!("{}", m.value);
    }
}

fn main() {
    reader::read();
}
Terminal
$ rustc --edition 2021 fix_sibling.rs -o fix_sibling
$ ./fix_sibling
7

The output is 7. pub(crate) makes the field visible everywhere inside the current crate and nowhere outside it, which matches this family's readers exactly: sibling modules in the same crate can read the field, while the field stays out of the crate's public surface. Marking the field plain pub would also repair the access, and in a binary crate the difference is invisible - but in a library it would publish the field to every downstream crate, a wider commitment than the one reader that needed it.

The fixes in reach order

  • Read the header's pair: the field name, and the struct that owns it. In a chained access the named struct is where the boundary sits - not necessarily the struct the expression started from.
  • Work out the accessing module's position relative to the defining module. The same module and its descendants can already reach the field; a parent, a sibling, the crate root, or another crate cannot.
  • No new reader should get the field: keep it private and reach it through a method - an accessor for a read, a setter for a write, a forwarding method on the outer struct for a nested read.
  • The readers are a known, bounded set: widen the field's visibility to exactly that set - pub(super) for the parent module, pub(crate) for the crate.
  • The field is plain public data with no invariant to protect: mark it pub, the error index's first option, and accept that its representation is now part of the type's surface.
  • The failing expression builds the value or takes it apart - a literal, a destructuring pattern - rather than reading a field: the code in the header is E0451, and the repair is to move construction into the module, not to widen the field for readers.
  • The header names a method, the struct itself, or a field that does not exist: the defect is E0624, E0603, or E0609 respectively, each repaired by its own rule.

Traps

  • Field access from inside the defining module compiles - the module's own functions read its structs' private fields freely. Here a function in mod meter reads the field, and main calls that function:
Rust
mod meter {
    pub struct Meter {
        value: i32,
    }

    pub fn make() -> Meter {
        Meter { value: 7 }
    }

    pub fn read(m: &Meter) -> i32 {
        m.value
    }
}

fn main() {
    let m = meter::make();
    println!("{}", meter::read(&m));
}
Terminal
$ rustc --edition 2021 trap_same_module.rs -o trap_same_module
$ ./trap_same_module
7
Trap
The output is 7. This is the shape every method-based fix in this guide relies on: the access itself never had to change, only its location. When E0616 appears on code that used to work, a common cause is an access that was moved out of the defining module - into main, into a helper module - during a refactor, leaving the field behind. - Field access from a descendant module compiles - privacy runs downward, so a module nested inside the defining module can also read its private fields. Here a probe module inside mod meter reads the field:
Rust
mod meter {
    pub struct Meter {
        value: i32,
    }

    pub fn make() -> Meter {
        Meter { value: 7 }
    }

    pub mod probe {
        pub fn read(m: &super::Meter) -> i32 {
            m.value
        }
    }
}

fn main() {
    let m = meter::make();
    println!("{}", meter::probe::read(&m));
}
Terminal
$ rustc --edition 2021 trap_descendant.rs -o trap_descendant
$ ./trap_descendant
7

The output is 7. This is the same rule the Reference states for test modules: a child module can reach its parent's private items, which is why unit tests written in a child module can inspect internals. The contrast with the fourth family is the direction - descendants reach up into the defining module's privates; the parent module reaches nowhere into its child's. - A tuple struct's numbered field is a field, and a private one gives E0616 itself. Here .0 is read from outside the defining module:

Rust
mod meter {
    pub struct Meter(i32);

    impl Meter {
        pub fn new(value: i32) -> Meter {
            Meter(value)
        }
    }
}

fn main() {
    let m = meter::Meter::new(7);
    println!("{}", m.0);
}
Terminal
$ rustc --edition 2021 trap_tuple.rs -o trap_tuple
error[E0616]: field `0` of struct `Meter` is private
  --> trap_tuple.rs:13:22
   |
13 |     println!("{}", m.0);
   |                      ^ private field

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0616`.

The header names the field as 0, and the span is a single caret under the digit. Tuple fields carry visibility exactly as named fields do, and an unmarked one is private. The repairs are the same set: an accessor method on the type, or a public field - writing the tuple field as pub in the definition, pub struct Meter(pub i32), makes this same read compile and print 7. - Building the struct with a literal outside its module fails - with E0451, not E0616. Here the value is constructed in main instead of being read:

Rust
mod meter {
    pub struct Meter {
        value: i32,
    }

    pub fn read(m: &Meter) -> i32 {
        m.value
    }
}

fn main() {
    let m = meter::Meter { value: 7 };
    println!("{}", meter::read(&m));
}
Terminal
$ rustc --edition 2021 trap_literal.rs -o trap_literal
error[E0451]: field `value` of struct `Meter` is private
  --> trap_literal.rs:12:28
   |
12 |     let m = meter::Meter { value: 7 };
   |                            ^^^^^ private field

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0451`.

The sentence in the header is identical to E0616's - same field, same struct, same "private field" note - and only the code differs. Construction is a separate operation from field access: a struct literal names every field it sets, so it needs every one of them visible, and a destructuring pattern that names a private field lands on this same code. The repair is not a wider field for readers but a home for construction: a constructor function inside the module, like the new and make functions throughout this guide, or visible fields. Widening visibility to silence E0451 publishes the field to readers as a side effect of a construction problem. - Calling a private method fails - with E0624, not E0616. Here the field is never named at all; the method that reads it is what is private:

Rust
mod meter {
    pub struct Meter {
        value: i32,
    }

    impl Meter {
        pub fn new(value: i32) -> Meter {
            Meter { value }
        }

        fn secret(&self) -> i32 {
            self.value
        }
    }
}

fn main() {
    let m = meter::Meter::new(7);
    println!("{}", m.secret());
}
Terminal
$ rustc --edition 2021 trap_method.rs -o trap_method
error[E0624]: method `secret` is private
  --> trap_method.rs:19:22
   |
11 |         fn secret(&self) -> i32 {
   |         ----------------------- private method defined here
...
19 |     println!("{}", m.secret());
   |                      ^^^^^^ private method

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0624`.

Methods carry their own visibility, separate from the fields they touch: secret reads the private field legally inside its module, and is itself private, so the call from main fails. Note the diagnostic's shape - unlike E0616, it carries a second span, "private method defined here", pointing back at the definition, because which item is private is the question this error answers. The repair is to mark the method pub or to call it from inside the module; no field visibility is involved on either side. - Naming a struct that is itself private fails - with E0603, before any field is reached. Here the field is even marked pub, and it makes no difference:

Rust
mod meter {
    struct Meter {
        pub value: i32,
    }

    pub fn make() -> Meter {
        Meter { value: 7 }
    }
}

fn main() {
    let m = meter::Meter { value: 7 };
    println!("{}", m.value);
}
Terminal
$ rustc --edition 2021 trap_private_struct.rs -o trap_private_struct
error[E0603]: struct `Meter` is private
  --> trap_private_struct.rs:12:20
   |
12 |     let m = meter::Meter { value: 7 };
   |                    ^^^^^ private struct
   |
note: the struct `Meter` is defined here
  --> trap_private_struct.rs:2:5
   |
 2 |     struct Meter {
   |     ^^^^^^^^^^^^

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0603`.

The span underlines the type's name in the path, and the note points at the definition. A pub field on a private struct is unreachable - visibility composes along the whole path, so the type has to be visible before any question about its fields arises. The repair is at the type: mark the struct pub, or keep it private and let callers work through functions inside the module that use the value there and hand out plain results, such as an i32 read from the field. A caller outside the module cannot work through a function like make that returns the struct itself: the call fails with type \Meter` is private`, because the returned value's type is not reachable at the call site. Reading the code in the header keeps the two apart - E0616 means the type was reachable and the field was not; E0603 means the type itself was not.

Checklist

  • The header's field-and-struct pair read as the diagnosis - in a chained access, the named struct is the one whose field boundary was crossed.
  • The accessing module's position checked against the defining module: same module and descendants compile; parent, sibling, crate root, and other crates do not.
  • A read repaired with an accessor method and a write with a setter method when the field itself stays private.
  • A nested read repaired at the wrapper's level, with a forwarding method, rather than by publishing the inner type's field.
  • Visibility widened only as far as the actual readers: pub(super) for a parent, pub(crate) for the crate, pub for genuine public data.
  • A literal or destructuring failure read as E0451 and repaired by moving construction into the module - a constructor - not by widening the field.
  • A method failure read as E0624, a private-type failure as E0603, and a field that does not exist at all as E0609, each repaired by its own rule.
  • Tuple struct fields treated as fields: .0 on a private tuple field is E0616, repaired the same ways.
  • The fixed program run, and its output checked against what the program was meant to print.

Sources

  • Rust, Error codes index: E0616 — for the definition of the error as an attempt to access a private field on a struct, its example of a pub struct Foo whose field x stays private inside its module, and the two repairs it names: set the field public, or add a getter function.
  • The Rust Reference, Visibility and privacy — for everything being private by default, for a private item being accessible by the current module and its descendants, and for the restricted visibilities: pub(crate) visible within the current crate and pub(super) visible to the parent module.
  • The Rust Programming Language, Characteristics of Object-Oriented Languages — for encapsulation through a pub struct with private fields, for the public methods being the only ways to access or modify the data in its AveragedCollection example, and for leaving fields private so the internals can change without callers changing.

Keep Exploring