]> git.lizzy.rs Git - rust.git/commitdiff
Add Early Return rule to style
authorAleksey Kladov <aleksey.kladov@gmail.com>
Mon, 24 Aug 2020 10:19:12 +0000 (12:19 +0200)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Mon, 24 Aug 2020 10:22:28 +0000 (12:22 +0200)
docs/dev/style.md

index 44f0956c247cb7c3a0555fcdb7f242cf05cdd4e5..bb7a351f3fa04f59e37b14325971c05d386eee4e 100644 (file)
@@ -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,