mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-25 20:43:21 +00:00
Style: default over new
This commit is contained in:
parent
84d6cdef86
commit
9c285b0341
1 changed files with 25 additions and 0 deletions
|
@ -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*.
|
||||
|
|
Loading…
Reference in a new issue