The pub
field allows a few more things that you may or may not want:
- Construction: users can create a
Stroke { duration: 42 }
.- This also means it's a breaking change for you to add or remove any fields.
- You can add a private field to avoid this, even just a dummy
_private: ()
that takes no space.
- Mutation: if the user has some
mut var: Stroke
or&mut Stroke
, they can change theduration
. This might be convenient, but you need to be careful if there are any invariants to uphold on that field. - Partial borrow: if a user binds
let var = &stroke.duration;
they can still use other fields of theStroke
without any complaint from the borrow checker.- This especially applies with mutation, since that's an exclusive borrow. With your getter, borrowing
duration
will lock the whole struct since it goes through the full&self
. The user could still do other immutable accesses, but not anything that wants mutable access to any part of the struct.
- This especially applies with mutation, since that's an exclusive borrow. With your getter, borrowing