Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAllow pattern matching after guard clause #45978
Comments
danielpclark
referenced this issue
Nov 14, 2017
Closed
RFC: Prefer Guard Clause when you can assert data rather than having Nested Conditionals #100
This comment has been minimized.
This comment has been minimized.
|
You could write this as: let f = match file {
Ok(f) => f,
Err(_) => return Err(Error::ConfigLoadFail),
};or, since let f = file.map_err(|_| Error::ConfigLoadFail)?;If we supported rust-lang/rfcs#1303 ( let Ok(f) = file else {
return Err(Error::ConfigLoadFail);
};I think the compiler should never support the original code by OP as this means we need to inspect the implementation of I'm not sure if we should allow: if let Err(_) = file {
return Err(Error::ConfigLoadFail);
}
let Ok(f) = file;this looks reasonable, but should require a proper RFC to clarify the control flow interaction. |
kennytm
added
C-feature-request
T-lang
labels
Nov 14, 2017
This comment has been minimized.
This comment has been minimized.
|
I'm not sure how the internals for Rust checking paths are implemented, but I'm suggesting this more as a relative Guard Clause thing rather than a In the example above the is a conditional check on What I mean by not strictly a if file.is_none() { return Err(Error::ConfigLoadFail); }
let Some(f) = file;I do love the |
This comment has been minimized.
This comment has been minimized.
|
What I mean is that the compiler should not be allowed to peek into the definition of if file.is_none() { return; }
let Some(f) = file;
// Unacceptable, the signature `fn is_none(&self) -> bool` tells nothing
// about the typestate of `file`but if let None = file { return; }
let Some(f) = file;
// Acceptable, we know that `Option` can only be
// `None` or `Some(_)`, and the `None` case will not reach here
// so we are sure that `file` can only be `Some(_)`
// and thus the pattern is irrefutable. |
This comment has been minimized.
This comment has been minimized.
Yes. I find that as very acceptable. Thank you. |
This comment has been minimized.
This comment has been minimized.
|
Shuldn't language feature requests live in the rfcs repo? |
This comment has been minimized.
This comment has been minimized.
|
@leodasvacas Yes, for major changes like this. Perhaps the discussion should continue in the Pre-RFC on the internals forum: https://internals.rust-lang.org/t/pre-rfc-allow-pattern-matching-after-guard-clause/6238 |
This comment has been minimized.
This comment has been minimized.
|
This seems like typestate which existed in Ancient History Rust. |
danielpclark
referenced this issue
Nov 15, 2017
Closed
Idiom: Prefer Guard Clause over Nested Conditionals #62
This comment has been minimized.
This comment has been minimized.
|
Triage: Broad feature request, should be moved to the RFCs repo. |
danielpclark commentedNov 14, 2017
I've opened a separate issue for a Rust style guide on Guard Clause. What this issue is about is that Rust currently doesn't recognize when a path has already been handled by a Guard Clause.
I want to do the following:
let Ok(f) = fileis a nice usage of pattern matching. But Rust complains as it doesn't know I've already accounted for the error path:I really look forward to using pattern matching with guard clauses rather than using
unwrap()which I find a bit of an eye sore.I love Rust, keep up the good work ^_^