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.

Rust error handling

47 pointsby bitfield8 months ago

9 comments

_nalply8 months ago
&gt; But, since good programs don’t panic, and neither do good programmers, it’s very rare that using unwrap or expect is actually the right thing to do.<p>I respectfully disagree.<p>The assert!() macro is a way to document invariants. It&#x27;s a bug if a variant is violated. It shouldn&#x27;t have happened, but if it happens, then there&#x27;s nothing the user can do except reporting the crash.<p>The unwrap() and expect() method document the invariant that the None and Err() variants shouldn&#x27;t occur.<p>It&#x27;s fail fast.<p>You should use error handling only if users would be able to handle the errors.<p>Tell the end user that the file they wanted to open is not readable, for example. Tell the users of your library that an error happened, like a parse error of a config file. And so on. Tell the users what they can fix themselves.<p>A bug in your library or program, that&#x27;s something different. Fail fast! And Rust panics are perfect for that.
评论 #41547372 未加载
评论 #41542829 未加载
Animats8 months ago
Maybe not perfect, but it seems to work out better than exceptions. Exceptions are a good idea which turned out to be too complicated.<p>A language has to use destructors to clean up for almost everything for this to work. &quot;?&quot; has no &quot;catch&quot; clause within the function. So if an object has an invariant, and that invariant must be restored on error, the destructors must restore the invariant. If that just means unlocking locks, closing files, or deleting data structures, that works. If some more complex invariant needs to be restored, &quot;?&quot; isn&#x27;t enough. The calling function has to clean things up. This usually means encapsulating the function that does the work in a function that handles errors and cleanup. Basically, a try block.
评论 #41544527 未加载
divan8 months ago
&quot;Go’s Error Handling Is Perfect, Actually&quot; [1]<p><a href="https:&#x2F;&#x2F;blog.verygoodsoftwarenotvirus.ru&#x2F;posts&#x2F;errors-in-go&#x2F;" rel="nofollow">https:&#x2F;&#x2F;blog.verygoodsoftwarenotvirus.ru&#x2F;posts&#x2F;errors-in-go&#x2F;</a>
评论 #41542259 未加载
评论 #41543153 未加载
评论 #41542613 未加载
pjmlp8 months ago
Except for anything more production quality, one needs to lean on third party crates to compose errors without explicitly write tons of boilerplate composing result types.<p>Something that should be supported directly.
评论 #41542563 未加载
WorkerBee284748 months ago
For those who want to experiment with this style in C#, I&#x27;ve found this package to work: <a href="https:&#x2F;&#x2F;github.com&#x2F;JohannesMoersch&#x2F;Functional">https:&#x2F;&#x2F;github.com&#x2F;JohannesMoersch&#x2F;Functional</a>
评论 #41544529 未加载
blastonico8 months ago
Isn&#x27;t it based on ML family? I mean, I see Rust error handling heavy inspired in monads used in languages like OCaml and Haskell.<p>Is Rust doing something different?
评论 #41543438 未加载
评论 #41542391 未加载
IshKebab8 months ago
I don&#x27;t know if I&#x27;d say it&#x27;s <i>perfect</i>. I&#x27;d still like a way to break on `Err` in debuggers for example.
XlA5vEKsMISoIln8 months ago
<p><pre><code> .site-page.loading { opacity: unset; }</code></pre>
brigadier1328 months ago
It&#x27;s far from perfect.<p>One of the biggest problems with Rust error handling is that if you want to have explicit error return types encoded in the type system you need to create ad hoc enums for each method that returns an error. If you only use a single error type for all functions you will inevitably have functions returning Results that contain error variants that the function will never actually return but still need to be handled.<p>Without enum variants being explicit types and the ability to easily create anonymous enums using union and intersection operators Rust enums require a ton of boilerplate.
评论 #41543183 未加载
评论 #41542593 未加载
评论 #41542283 未加载