smart constructor
A constructor that checks its input and refuses to build an invalid value.
A smart constructor (fn new(...) -> Result<Self, Error>) with private fields is the only way to create the type, so every value satisfies its invariants.
Examples
pub struct ItemCode(String);with a private field:ItemCode(String::from("banana"))outside the module fails with E0423, so callers must go through the checking constructor.
See also