Hacker News new | ask | show | jobs
by bonzini 663 days ago
Rust Result is basically a checked exception. Java makes you choose between adding an exception to "throws" or catching it, Rust makes you choose between declaring your function as returning Result or checking if you got an Err.

The only difference is that Rust has better syntactic sugar for the latter, but Result is really isomorphic to Java checked exceptions.

Panic could be said to be the same as an unchecked exception, except you have a lot more control on what causes them. The panic you get from calling unwrap() on an Option is the same as a NullPointerException, but you have full control on which points of the program that can generate it.

2 comments

Rust goes to substantial lengths to allow unwinding from panics. For example, see how complicated `Vec::retain_mut` is. The complexity is due to the possibility of a panic, and the need to unwind.

https://doc.rust-lang.org/1.80.1/src/alloc/vec/mod.rs.html#1...

I’ve never written any Java so your comparisons are lost on me.

Rust Result is great. I love it.

The root article was talking about C++. Rust panic is basically the same as a C++ exception afaict. With the caveat that Rust discourages catching and resuming from panics. But you can!