rust-unofficial / patterns

A catalogue of Rust design patterns, anti-patterns and idioms
https://rust-unofficial.github.io/patterns/
Mozilla Public License 2.0
7.98k stars 363 forks source link

State scope of builder pattern #122

Open simonsan opened 3 years ago

simonsan commented 3 years ago

Perhaps the builder pattern doc should mention that this is about initializing private struct fields. There is no point in using builder (or multiple constructors which the builder pattern is supposed to help to avoid) if all fields are public.

Originally posted by @zoechi in https://github.com/rust-unofficial/patterns/issues/64#issuecomment-547826968

Also mentioning a complementary relationship between a Default-trait and the builder pattern could be a nice addition?

I think it's fair to say that default impls and builders complement each other. It's often useful to use both in some combination. The derive builder crate supports this. In specific cases you may only need one or the other. I don't think either in isolation in all cases is a silver bullet for struct creation.

Originally posted by @softprops in https://github.com/rust-unofficial/patterns/issues/64#issuecomment-352286935