3 ms·
Err, you can't use `?` with `Option` unless I've really missed a big feature somewhere. You'd have to map it to a `Result` then unwrap, but at that point you st
by Klonoar 11mo ago
Err, you can't use `?` with `Option` unless I've really missed a big feature somewhere. You'd have to map it to a `Result` then unwrap, but at that point you still need to define an error type.
- tialaramex 11mo agohttps://doc.rust-lang.org/std/option/enum.Option.html#impl-Try-for-Option%3CT%3E https://doc.rust-lang.org/std/option/enum.Option.html#impl-T... I guess you've "really missed" this feature or more likely you are considering only the case where our function signature says we return Result and so early returning Option won't compile. Because the modern "try v2" operator is a trait, it is implemented for Option, Result, and ControlFlow plus Poll variations. Historically its ancestor the try! macro was provided only for Result, but that's a very long time ago now.
- Klonoar 11mo agoI guess? If it's still languishing in nightly I'm not sure I consider it usable; I'm aware some people are fine to run nightly but I keep to stable releases.
- tialaramex 11mo agoSo, all of Try is unstable. This means your stable Rust isn't allowed to implement Try for your own custom types, however the syntax is stable, so since Try is implemented for all of Option, Result and ControlFlow, those all work with the ? syntax even though that implementation is marked unstable. Let me give you another example you've probably used without realising it, when you write password.contains("foo") in Rust. Why can you write "foo" there? Rust doesn't have overloading, and yet you can pass strings, characters, and functions including lambdas - how can this work? They all implement core::str::pattern::Pattern, an unstable trait used to deliver this functionality for all string matching. In fact mechanically what's happening here for Try is that ? always worked for Option and Result, the "try v2" feature isn't about the syntax at all, it's about the behind the scenes implementation of that syntax. But that's not important, the syntax is promised and the final implementation will almost certainly be tryv2 or something very similar.
- Klonoar 11mo agoTIL. Thanks for noting!
- IshKebab 11mo agoYou have missed a big feature! They added support for that like a year ago or something.
- tialaramex 11mo agoYears ago. In April 2021 this starts to appear as part of Try v2 implementation work.
- Klonoar 11mo agoI guess? If it's still languishing in nightly I'm not sure I consider it usable; I'm aware some people are fine to run nightly but I keep to stable releases.
- IshKebab 11mo agoNo it's stable.