mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-15 09:27:27 +00:00
Merge #6233
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:
commit
b2205bd107
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