aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-08-24 11:19:12 +0100
committerAleksey Kladov <[email protected]>2020-08-24 11:22:28 +0100
commitd7ece3028de09f30af4384726eca1eff2cd52dff (patch)
treeaba886ff55f59399c1affebc3bbf867b5b2573af
parente65d48d1fb3d4d91d9dc1148a7a836ff5c9a3c87 (diff)
Add Early Return rule to style
-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 44f0956c2..bb7a351f3 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,