1 Attempted to access a private field on a struct.
3 Erroneous code example:
8 x: u32, // So `x` is private in here.
12 pub fn new() -> Foo { Foo { x: 0 } }
16 let f = some_module::Foo::new();
17 println!("{}", f.x); // error: field `x` of struct `some_module::Foo` is private
20 If you want to access this field, you have two options:
22 1) Set the field public:
27 pub x: u32, // `x` is now public.
31 pub fn new() -> Foo { Foo { x: 0 } }
35 let f = some_module::Foo::new();
36 println!("{}", f.x); // ok!
39 2) Add a getter function:
44 x: u32, // So `x` is still private in here.
48 pub fn new() -> Foo { Foo { x: 0 } }
50 // We create the getter function here:
51 pub fn get_x(&self) -> &u32 { &self.x }
55 let f = some_module::Foo::new();
56 println!("{}", f.get_x()); // ok!