Builder Struct or Closure Parameter?

There’s two design ideas for this right now:

let embed = Embed::new().title("Foo");
CTX.send_message(, embed).await?;
CTX.send_message(, |e| e.title("Foo")).await?;

Which one is (or is something else) more preferable?

If the builder is easy to construct and doesn't have any invariants that you'd need to use a closure to enforce, I would tentatively lean towards the first option. Closures can be very convenient. But they also have some constraints in Rust that can make them kind of annoying in some cases due to the ownership rules.

Note that as long as the builder methods all return Embed in both cases you can rewrite the first example as

CTX.send_message(, Embed::new().title("Foo")).await?;

So you're only saving the import of Embed and the Embed::new() at the call site, which I don't really think is much of a significant difference.

1 Like

Thank you! Explains everything nicely

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.