TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

The Try Block in Rust

3 pointsby nfrankelabout 1 year ago

1 comment

estebankabout 1 year ago
FWIW, if I had to avoid the `Result` return type and produce the `-1` sentinel, I would write<p><pre><code> fn add(str1: &amp;str, str2: &amp;str) -&gt; i8 { let (Ok(a), Ok(b)) = (str1.parse::&lt;i8&gt;(), str2.parse::&lt;i8&gt;()) else { return -1; }; a + b } </code></pre> `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&#x27;t yet been worked out. A &quot;cute&quot; thing I&#x27;ve seen people do is use closures as a pseudo-try scope, but I tend to not do that.