6233: Style: default over new r=matklad a=matklad

bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-10-14 18:02:36 +00:00 committed by GitHub
commit b2205bd107
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -186,6 +186,31 @@ impl Person {
}
```
## Constructors
Prefer `Default` to zero-argument `new` function
```rust
// Good
#[derive(Default)]
struct Foo {
bar: Option<Bar>
}
// Not as good
struct Foo {
bar: Option<Bar>
}
impl Foo {
fn new() -> Foo {
Foo { bar: None }
}
}
```
Prefer `Default` even it has to be implemented manually.
## Avoid Monomorphization
Rust uses monomorphization to compile generic code, meaning that for each instantiation of a generic functions with concrete types, the function is compiled afresh, *per crate*.