aboutsummaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2021-03-16 07:51:05 +0000
committerAleksey Kladov <[email protected]>2021-03-16 07:51:05 +0000
commit30dea3a727879f9ed6cfe06433f1adb51765fa9e (patch)
tree5149759011cd3b4f034d5b36b3797aa59c722be2 /docs
parentd38fd77845c40c6f07507c5c436af903a452efbd (diff)
Prefer match to if let else
Diffstat (limited to 'docs')
-rw-r--r--docs/dev/style.md21
1 files changed, 21 insertions, 0 deletions
diff --git a/docs/dev/style.md b/docs/dev/style.md
index c4eb7bc7a..e4a1672ca 100644
--- a/docs/dev/style.md
+++ b/docs/dev/style.md
@@ -787,6 +787,27 @@ assert!(0 > x);
787 787
788**Rationale:** Less-then comparisons are more intuitive, they correspond spatially to [real line](https://en.wikipedia.org/wiki/Real_line). 788**Rationale:** Less-then comparisons are more intuitive, they correspond spatially to [real line](https://en.wikipedia.org/wiki/Real_line).
789 789
790## If-let
791
792Avoid `if let ... { } else { }` construct, use `match` instead.
793
794```rust
795// GOOD
796match ctx.expected_type.as_ref() {
797 Some(expected_type) => completion_ty == expected_type && !expected_type.is_unit(),
798 None => false,
799}
800
801// BAD
802if let Some(expected_type) = ctx.expected_type.as_ref() {
803 completion_ty == expected_type && !expected_type.is_unit()
804} else {
805 false
806}
807```
808
809**Rational:** `match` is almost always more compact.
810The `else` branch can get a more precise pattern: `None` or `Err(_)` instead of `_`.
790 811
791## Token names 812## Token names
792 813