aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-08-24 11:56:04 +0100
committerGitHub <[email protected]>2020-08-24 11:56:04 +0100
commit951c7c157af1c70392492ab1bfd6fb291d2ffefb (patch)
tree76296289b9047492b22eca1c0195d363973af609
parent6eddcfd7a5e62c5e0b188fcdb084b8147cd7c62c (diff)
parentd7ece3028de09f30af4384726eca1eff2cd52dff (diff)
Merge #5852
5852: Add Early Return rule to style r=matklad a=matklad bors r+ Co-authored-by: Aleksey Kladov <[email protected]>
-rw-r--r--docs/dev/style.md26
1 files changed, 25 insertions, 1 deletions
diff --git a/docs/dev/style.md b/docs/dev/style.md
index ae69cf1a7..bb99c4855 100644
--- a/docs/dev/style.md
+++ b/docs/dev/style.md
@@ -181,6 +181,30 @@ fn frobnicate(walrus: Option<Walrus>) {
181} 181}
182``` 182```
183 183
184# Early Returns
185
186Do use early returns
187
188```rust
189// Good
190fn foo() -> Option<Bar> {
191 if !condition() {
192 return None;
193 }
194
195 Some(...)
196}
197
198// Not as good
199fn foo() -> Option<Bar> {
200 if condition() {
201 Some(...)
202 } else {
203 None
204 }
205}
206```
207
184# Getters & Setters 208# Getters & Setters
185 209
186If a field can have any value without breaking invariants, make the field public. 210If a field can have any value without breaking invariants, make the field public.
@@ -189,7 +213,7 @@ Never provide setters.
189 213
190Getters should return borrowed data: 214Getters should return borrowed data:
191 215
192``` 216```rust
193struct Person { 217struct Person {
194 // Invariant: never empty 218 // Invariant: never empty
195 first_name: String, 219 first_name: String,