Visibility and Encapsulation
Like with items in a module, struct fields are also private by default. Private fields are likewise visible within the rest of the module (including child modules). This allows us to encapsulate implementation details of struct, controlling what data and functionality is visible externally.
Speaker Notes
This slide should take about 5 minutes.
-
This slide demonstrates how privacy in structs is module-based. Students coming from object oriented languages may be used to types being the encapsulation boundary, so this demonstrates how Rust behaves differently while showing how we can still achieve encapsulation.
-
Note how the
is_big
field is fully controlled byFoo
, allowingFoo
to control how it's initialized and enforce any invariants it needs to (e.g. thatis_big
is onlytrue
ifval > 100
). -
Point out how helper functions can be defined in the same module (including child modules) in order to get access to the type's private fields/methods.
-
The first commented out line demonstrates that you cannot initialize a struct with private fields. The second one demonstrates that you also can't directly access private fields.
-
Enums do not support privacy: Variants and data within those variants is always public.
More to Explore
-
If students want more information about privacy (or lack thereof) in enums, you can bring up
#[doc_hidden]
and#[non_exhaustive]
and show how they're used to limit what can be done with an enum. -
Module privacy still applies when there are
impl
blocks in other modules (example in the playground).