A question recently came up at work that I don't have a strong answer to: when should one use the builder pattern vs some sort of options struct?
For example, what is the big difference between these two, and when should one be preferred over the other?
struct Foo {
r1: u32,
r2: u32,
o1: Option<u32>,
o2: Option<u32>,
}
// OPTION 1:
struct FooBuilder {
r1: u32,
r2: u32,
o1: Option<u32>,
o2: Option<u32>,
}
impl FooBuilder {
fn new(r1: u32, r2: u32) -> Self;
fn with_o1(self) -> Self;
fn with_o2(self) -> Self;
fn build(&self) -> Foo;
}
// OPTION 2:
// this could have some similar methods to the builder if desired
struct FooOptions {
o1: Option<32>,
o2: Option<32>,
}
impl Foo {
fn new(r1: u32, r2: u32, options: FoOptions) -> Self;
}