4 ms·
The Try Block in Rust
- estebank 2y agoFWIW, if I had to avoid the `Result` return type and produce the `-1` sentinel, I would write fn add(str1: &str, str2: &str) -> i8 { let (Ok(a), Ok(b)) = (str1.parse::<i8>(), str2.parse::<i8>()) else { return -1; }; a + b } `Try` is nightly-only, and has open design questions around precisely the issue highlighted in the article: inference breaks down in the face of `try`, and the proper syntax to use or changes to the algorithm to aid the inference machinery hasn't yet been worked out. A "cute" thing I've seen people do is use closures as a pseudo-try scope, but I tend to not do that.