diff options
| author | Aleksey Kladov <aleksey.kladov@gmail.com> | 2020-08-24 12:19:12 +0200 |
|---|---|---|
| committer | Aleksey Kladov <aleksey.kladov@gmail.com> | 2020-08-24 12:22:28 +0200 |
| commit | d7ece3028de09f30af4384726eca1eff2cd52dff (patch) | |
| tree | aba886ff55f59399c1affebc3bbf867b5b2573af /docs | |
| parent | e65d48d1fb3d4d91d9dc1148a7a836ff5c9a3c87 (diff) | |
| download | rust-d7ece3028de09f30af4384726eca1eff2cd52dff.tar.gz rust-d7ece3028de09f30af4384726eca1eff2cd52dff.zip | |
Add Early Return rule to style
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/dev/style.md | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/docs/dev/style.md b/docs/dev/style.md index 44f0956c247..bb7a351f3fa 100644 --- a/docs/dev/style.md +++ b/docs/dev/style.md @@ -181,6 +181,30 @@ fn frobnicate(walrus: Option<Walrus>) { } ``` +# Early Returns + +Do use early returns + +```rust +// Good +fn foo() -> Option<Bar> { + if !condition() { + return None; + } + + Some(...) +} + +// Not as good +fn foo() -> Option<Bar> { + if condition() { + Some(...) + } else { + None + } +} +``` + # Getters & Setters If a field can have any value without breaking invariants, make the field public. @@ -189,7 +213,7 @@ Never provide setters. Getters should return borrowed data: -``` +```rust struct Person { // Invariant: never empty first_name: String, |
