Should the associated function options
become a common API convention?
#260
Replies: 2 comments
-
"options" is a somewhat idiosyncratic name for what it is: a builder. For types Foo and FooBuilder, yes, I think it is nice to have a Foo::builder method. I personally plan to add such a thing to most or all of my crates, but just haven't actually done it yet. |
Beta Was this translation helpful? Give feedback.
-
I argue a trait could be useful on this matter: trait Builder {
type Builder;
fn builder() -> Self::Builder;
} It's very simple and some could say it's useless, but it's allow documentation easy thing and set a more clear convention. I'm opposed to use |
Beta Was this translation helpful? Give feedback.
-
The function
File::options
was stabilized in Rust 1.58 (rust-lang/rust#65439). The arguments given for this function to exist is that it's syntactic sugar forOpenOptions::new()
that does not require importingOpenOptions
.rust-lang/rust#65439 (comment)
This raises the question: for other builder types of the
FooOptions
form, should it become idiomatic for Rust code to also add an associated functionFoo::options()
that is equivalent in behavior asFooOptions::new()
, and how would it be encoded as a guideline? This does not appear to be something ubiquitous at this time, but the introduction of this pattern instd
creates a precedent.Beta Was this translation helpful? Give feedback.
All reactions