Style: default over new

This commit is contained in:
Aleksey Kladov 2020-10-14 20:02:03 +02:00
parent 84d6cdef86
commit 9c285b0341

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 ## 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*. 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*.