IssueRaiser

Allow negation of `if let`

The RFC for `if let` was accepted with the rationale that it lowers the boilerplate and improves ergonomics over the equivalent `match` statement in certain cases, and I wholeheartedly agree. However, some cases still remain exceedingly awkward; an example of which is attempting to match the "class" of a record `enum` variant, e.g. given the following enum: ``` enum Foo { Bar(u32), Baz(u32), Qux } ``` and an instance `foo: Foo`, with behavior predicated on `foo` _not_ being `Bar` and a goal of minimizing nesting/brace creep (e.g. for purposes of an early return), the only choice is to type out something like this: ```rust // If we are not explicitly using Bar, just return now if let Foo::Bar(_) = self.integrity_policy { } else { return Ok(()); } // flow resumes here ``` or the equally awkward empty `match` block: ```rust // If we are not explicitly using Bar, just return now match self.integrity_policy { Foo::Bar(_) => return Ok(()); _ => {} } // flow resumes here ``` --- It would be great if this were allowed: ```rust // If we are not explicitly using Bar, just return now if !let Foo::Bar(_) = self.integrity_policy { return Ok(()); } // flow resumes here ``` or perhaps a variation on that with slightly less accurate mathematical connotation but far clearer in its intent (you can't miss the `!` this time): ```rust // If we are not explicitly using Bar, just return

Posted by mqudsi on Dec 22, 2018, 2:31 AM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.4,000 issues0 comments

github (Other)

Allow negation of `if let`

mqudsiDec 22, 2018, 2:31 AM

The RFC for `if let` was accepted with the rationale that it lowers the boilerplate and improves ergonomics over the equivalent `match` statement in certain cases, and I wholeheartedly agree. However, some cases still remain exceedingly awkward; an example of which is attempting to match the "class" of a record `enum` variant, e.g. given the following enum: ``` enum Foo { Bar(u32), Baz(u32), Qux } ``` and an instance `foo: Foo`, with behavior predicated on `foo` _not_ being `Bar` and a goal of minimizing nesting/brace creep (e.g. for purposes of an early return), the only choice is to type out something like this: ```rust // If we are not explicitly using Bar, just return now if let Foo::Bar(_) = self.integrity_policy { } else { return Ok(()); } // flow resumes here ``` or the equally awkward empty `match` block: ```rust // If we are not explicitly using Bar, just return now match self.integrity_policy { Foo::Bar(_) => return Ok(()); _ => {} } // flow resumes here ``` --- It would be great if this were allowed: ```rust // If we are not explicitly using Bar, just return now if !let Foo::Bar(_) = self.integrity_policy { return Ok(()); } // flow resumes here ``` or perhaps a variation on that with slightly less accurate mathematical connotation but far clearer in its intent (you can't miss the `!` this time): ```rust // If we are not explicitly using Bar, just return

Comments (0)

No comments yet. Start the discussion.

Comment on this issue