Builder pattern for complex struct initialization

12721
0

For structs with many optional fields, the builder pattern provides a fluent API for construction. I define a separate Builder struct with methods that return self for chaining. The final build() method validates and returns the target struct. This is more ergonomic than constructors with many arguments, and it allows validation logic (required fields, invariants). The derive_builder crate can auto-generate builders, but for complex cases I write them manually. I use builders for config structs, clients with many options, and anywhere you'd use optional named parameters in other languages. The pattern also makes it easy to add new fields without breaking existing code, improving API stability.