Inside a module:
mod foo {
struct Foo {
a: i16
}
}
let x = foo::Foo {a: 12};
x.a[E0603] Error: struct `Foo` is private
╭─[ command_2:1:1 ]
│
7 │ let x = foo::Foo {a: 12};
│ ─┬─
│ ╰─── private struct
│
├─[ command_2:1:1 ]
│
2 │ struct Foo {
│ ─────┬────
│ ╰────── note: the struct `Foo` is defined here
───╯[E0616] Error: field `a` of struct `Foo` is private
╭─[ command_2:1:1 ]
│
8 │ x.a
│ ┬
│ ╰── private field
───╯mod foo {
pub struct Foo {
a: i16
}
}
let x = foo::Foo {a: 12};
x.a[E0616] Error: field `a` of struct `Foo` is private
╭─[ command_3:1:1 ]
│
8 │ x.a
│ ┬
│ ╰── private field
───╯mod foo {
pub struct Foo {
pub a: i16
}
}
let x = foo::Foo {a: 12};
x.a12Using constructor function new():
mod foo {
pub struct Foo {
a: i16
}
impl Foo {
pub fn new(value: i16) -> Self {
Self { a: value }
}
pub fn a(&self) -> i16 {
self.a
}
}
}
let x = foo::Foo::new(12);
x.a()The type of the variable x was redefined, so was lost.
12