struct Builder
In my last post I started to explore using the builder pattern in Rust. In this post I want to go a bit more in depth about what a builder is and why it can be a versatile, idiomatic tool. They are especially helpful in situations where you might otherwise reach for patterns you are used to coming from another language and be wondering how to do the same in Rust without falling into an anti-pattern or battling the borrow checker.
Builders are a handy and idiomatic pattern to use when:
- You want to use something like overloading (if you are coming from an Object Oriented Language)
- would like to have
defaultvalues that can be overwritten when needed at creation - Have a large number of possible inputs
- Want optional add-ons
- or anywhere you don't want a
new(..)method that looks like a grocery list.