Skip to content

Commit 951c7c1

Browse files
bors[bot]matklad
andauthored
Merge #5852
5852: Add Early Return rule to style r=matklad a=matklad bors r+ Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 6eddcfd + d7ece30 commit 951c7c1

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

docs/dev/style.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,30 @@ fn frobnicate(walrus: Option<Walrus>) {
181181
}
182182
```
183183

184+
# Early Returns
185+
186+
Do use early returns
187+
188+
```rust
189+
// Good
190+
fn foo() -> Option<Bar> {
191+
if !condition() {
192+
return None;
193+
}
194+
195+
Some(...)
196+
}
197+
198+
// Not as good
199+
fn foo() -> Option<Bar> {
200+
if condition() {
201+
Some(...)
202+
} else {
203+
None
204+
}
205+
}
206+
```
207+
184208
# Getters & Setters
185209

186210
If a field can have any value without breaking invariants, make the field public.
@@ -189,7 +213,7 @@ Never provide setters.
189213

190214
Getters should return borrowed data:
191215

192-
```
216+
```rust
193217
struct Person {
194218
// Invariant: never empty
195219
first_name: String,

0 commit comments

Comments
 (0)