A pub struct's fields are public, except fields whose type is private to its module, and the
fields of a non-pub struct. This code outside the module read or wrote such a field.
Wrong#
[package]
name = "app"
version = "0.1.0"struct Secret { code: int }
pub struct Item { name: str, s: Secret }
pub fn make(): Item = Item { name: "auk", s: Secret { code: 7 } }fn main() { print(shop.make().s) }Fixed#
fn main() { print(shop.make().name) }Common causes#
- A field whose type is private: make the type
pub, or expose a function that returns what callers need. - Destructuring a struct from another module with a pattern that names a private field.