diff --git a/docs/dev/style.md b/docs/dev/style.md index 44f0956c24..bb7a351f3f 100644 --- a/docs/dev/style.md +++ b/docs/dev/style.md @@ -181,6 +181,30 @@ fn frobnicate(walrus: Option) { } ``` +# Early Returns + +Do use early returns + +```rust +// Good +fn foo() -> Option { + if !condition() { + return None; + } + + Some(...) +} + +// Not as good +fn foo() -> Option { + 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,