Skip to content

Commit

Permalink
Merge pull request #1185 from peter-kehl/1184
Browse files Browse the repository at this point in the history
For #1184 closes #1184
  • Loading branch information
marioidival authored Apr 24, 2019
2 parents 68ea088 + c75b3d4 commit 7775157
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/flow_control/if_let.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,26 @@ fn main() {
}
```

Another benefit: `if let` allows to match enum non-parameterized variants, even if the enum doesn't `#[derive(PartialEq)]`, neither we implement `PartialEq` for it. In such case, classic `if Foo::Bar==a` fails, because instances of such enum are not comparable for equality. However, `if let` works.

Would you like a challenge? Fix the following example to use `if let`:

```rust,editable,ignore
// This enum purposely doesn't #[derive(PartialEq)],
// neither we implement PartialEq for it. That's why comparing Foo::Bar==a fails below.
enum Foo {Bar}
fn main() {
let a = Foo::Bar;
// Variable a matches Foo::Bar
if Foo::Bar == a {
// ^-- this causes a compile-time error. Use `if let` instead.
println!("a is foobar");
}
}
```

### See also:

[`enum`][enum], [`Option`][option], and the [RFC][if_let_rfc]
Expand Down

0 comments on commit 7775157

Please sign in to comment.