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
- struct's lifetime requirements can be bypassed via covariant field assignment
- Proposal to Reorganize the Moderation Team and Review Moderation Policies
- Progressive FPS degradation appears to be related to dota2.com — blocking the domain prevents the performance loss
- proposal: container/...: generic collection types
- Better Custom Endpoint BYOK
- A concrete policy for backward compatibility until Godot 5.0
Raise real-world problems, find people facing the same issue, and discuss solutions.4,000 issues0 comments
github (Other)
struct's lifetime requirements can be bypassed via covariant field assignment
Proposal to Reorganize the Moderation Team and Review Moderation Policies
Progressive FPS degradation appears to be related to dota2.com — blocking the domain prevents the performance loss
proposal: container/...: generic collection types
Better Custom Endpoint BYOK
A concrete policy for backward compatibility until Godot 5.0
New MTP models not supported
Tracking: 429 / Capacity Issues
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
Comments (0)
No comments yet. Start the discussion.