4 ms·
Something I use for situations where nesting is getting out of hand. Probably not idiomatic but, I find it practical in these cases. if param.is_none() {
by the__alchemist 1y ago
Something I use for situations where nesting is getting out of hand. Probably not idiomatic but, I find it practical in these cases.
if param.is_none() {
// Handle, and continue, return an error etc
}
let value = param.as_ref().unwrap(); // or as_mut
// Use `value` as normal.
- acjohnson55 1y agoAny advantages of this over let-else?
- the__alchemist 1y agoI wasn't familiar with let-else...
- acjohnson55 1y agoI just learned about it through this article, myself!
- the__alchemist 1y agoUpdate: No, I don't think so. Just tried Let Else... I like it more than the ex I posted. That said, much like it took me a while to Grok if let syntax... this will be a struggle. See the other example in this thread where the one suggesting it reversed the syntax. That will take a bit to get right...
- rav 1y agoYou can rewrite it using let-or-else to get rid of the unwrap, which some would find to be more idiomatic. let value = Some(param.as_ref()) else { // Handle, and continue, return an error etc } // Use `value` as normal.
- the__alchemist 1y agoThat part intrigued me about the article: I hadn't heard of that syntax! Will try.
- epidemian 1y agoSmall nit: the Some() pattern should go on the left side of the assignment: let Some(value) = param.as_ref() else { // Handle, and continue, return an error etc } // Use `value` as normal.