4 ms·
Does anyone know why the last two lines in main() fail? AFAICS, v has the same type as both functions. fn try_option_some() -> Option<u8> { let val
by alfiedotwtf 9y ago
Does anyone know why the last two lines in main() fail? AFAICS, v has the same type as both functions.
fn try_option_some() -> Option<u8> {
let val = Some(1)?;
Some(val)
}
fn try_option_none() -> Option<u8> {
let val = None?;
Some(val)
}
fn main() {
assert_eq!(try_option_some(), Some(1));
assert_eq!(try_option_none(), None);
let v: Option<u8> = None;
assert_eq!(v, Some(None));
}
One other thing troubling me here - shouldn't try_option_none() return `None` and not `Some(None)`?
Looking at the diff for the commit, there's now a NoneError? Is there any documentation updates to look at how this works? Because AFAIK, now all matches will have to explicitly be updated to check for NoneError, otherwise they won't compile (since matches are not complete)?
- steveklabnik 9y agoAs the error says, = note: expected type `std::option::Option<u8>` found type `std::option::Option<std::option::Option<_>>` The return types of those functions are Option<u8>, but Some(None) is an Option<Option<_>>. > there's now a NoneError? It's not stable yet. > now all matches will have to explicitly be updated to check for NoneError NoneError only comes into play when you have a function that returns Result, but uses ? on an Option in the body. That code never compiled (and still doesn't on stable until Try is stabilized, and only if you have Result<T, NoneError> as your return type or some Err type that implements From<NoneError>, which you also can't write until it's stable. Does that help?
- alfiedotwtf 9y agoAh, awesome. Makes total sense now. Thanks!