diff options
| author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-10-14 18:02:36 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-10-14 18:02:36 +0000 |
| commit | b2205bd1079c2f517a8a504eac6bafc713eb6fef (patch) | |
| tree | 2e73cabbf233019601f8f19c8c38625f10305ca8 /docs/dev | |
| parent | 84d6cdef86dfe1054ecafaedfddbf90a2b3a469d (diff) | |
| parent | 9c285b0341fcf5ce9e15821da20727a313624497 (diff) | |
| download | rust-b2205bd1079c2f517a8a504eac6bafc713eb6fef.tar.gz rust-b2205bd1079c2f517a8a504eac6bafc713eb6fef.zip | |
Merge #6233
6233: Style: default over new r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
Diffstat (limited to 'docs/dev')
| -rw-r--r-- | docs/dev/style.md | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/docs/dev/style.md b/docs/dev/style.md index 59067d23460..435de63e3bd 100644 --- a/docs/dev/style.md +++ b/docs/dev/style.md @@ -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*. |
