

Yes, it’s not the same since you get a stacktrace (if enabled) and a message, but it’s the closest thing you get in safe rust (outside compiler bugs). I compare it to a segfault because it’s almost as unhandleble.
Basically, you don’t want a panic to crash your program in most cases. If you do, make it explicit (i.e. with expect()). unwrap() tells me the value is absolutely there or the dev is lazy, and I always assume the latter unless there’s an explanation (or it’s obvious from context) otherwise.




No, I responded to the relevant part. I was using
segfaultas a metaphor, not arguing that it’s actually the same mechanism underneath. If you’re getting panics in production code, I consider that just as much of an emergency to fix as a segfault, and Rust helpfully gives you stack trace info with it. It’s not the same idea as an exception, which could signify an unrecoverable error or an expected issue that can be recovered from.It forces you to write a message, so most temporary uses will be
unwrap(). I useunwrap()all the time when prototyping for the happy path, and then do proper error handling later. This is especially true in larger projects where I can’t just throw inanyhowor something and actually need to map error types and whatnot. I don’t useexpect()much (current hobby project has 4 uses, 3 for startup issues and 1 for hopefully impossible condition) but I think it makes sense when there’s no way to continue.But yes,
unwrap()is perhaps the first thing I look for as a reviewer, which is why it’s so surprising that this is the issue. At the very least, it should have been something likeexpect("exceeds max file size"). I personally prefer explicit panics in production code, but expect is close enough that it’s personal preference.