diff options
| author | Aleksey Kladov <aleksey.kladov@gmail.com> | 2021-09-04 12:56:34 +0300 |
|---|---|---|
| committer | Aleksey Kladov <aleksey.kladov@gmail.com> | 2021-09-04 16:56:23 +0300 |
| commit | 2d2c4e7c225424a47dff36c67dbd114f2178b764 (patch) | |
| tree | d00be1a6e427cc202d3a5d18c941583166116f46 /docs/dev | |
| parent | 33199b7e434d9d2bd5ba7a14c8c16ef981a0eda7 (diff) | |
| download | rust-2d2c4e7c225424a47dff36c67dbd114f2178b764.tar.gz rust-2d2c4e7c225424a47dff36c67dbd114f2178b764.zip | |
internal: deduplicate
Diffstat (limited to 'docs/dev')
| -rw-r--r-- | docs/dev/style.md | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/docs/dev/style.md b/docs/dev/style.md index 6131cdcbdce..92e79508b6d 100644 --- a/docs/dev/style.md +++ b/docs/dev/style.md @@ -895,6 +895,29 @@ fn foo() -> Option<Bar> { **Rationale:** reduce cognitive stack usage. +Use `return Err(err)` to throw an error: + +```rust +// GOOD +fn f() -> Result<(), ()> { + if condition { + return Err(()); + } + Ok(()) +} + +// BAD +fn f() -> Result<(), ()> { + if condition { + Err(())?; + } + Ok(()) +} +``` + +**Rationale:** `return` has type `!`, which allows the compiler to flag dead +code (`Err(...)?` is of unconstrained generic type `T`). + ## Comparisons When doing multiple comparisons use `<`/`<=`, avoid `>`/`>=`. |
