-
Notifications
You must be signed in to change notification settings - Fork 965
Rust Style Conventions #692
Copy link
Copy link
Closed
Description
My understanding of naming:
* `new` - obvious constructor, may be empty (`String::new()`, `Box::new(T)`) * `from_` - takes _same_ information as `new` just in different representation (e.g. `String::from_utf8(Vec<u8>)`) * `with_` - takes _additional_ information along with what `new` takes `String::with_capacity(usize)`
I thought String was a bit of a special case but a quick look at the std lib shows its not. Perhaps conventions have changed for more recent code? I was under the impression that
- Constructors with zero args should use
Default - Constructors with one arg should use
From<Foo>(unless the foo needs further restriction like theString;:from_utf8case) - Constructors with multiple args should use
new. - Constructors using the builder pattern should use
with(self, foo: Foo)
I've never managed to work out the pattern for times when we need multiple constructors with multiple args?
Amusingly I was looking at all the instances of single arg constructors for Script yesterday wondering why they used the pattern new_.
I don't mean to bike shed here, I'm genuinely interested in these sorts of conventions.
Originally posted by @tcharding in #677 (comment)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels