News for hackers. Stuff that matters.
Re:Making Rust binaries smaller by default by robinsonrc on Wednesday January 24, 2024 @05:01AM (#39115587)
I remember my disappointment when I found out panics could be “caught” and that I was still living in the world of exceptions
Re:Making Rust binaries smaller by default by kibwen on Wednesday January 24, 2024 @07:13AM (#39116364)
Panics have to be catchable because it's undefined behavior for Rust to unwind through C code, so a Rust library that exposes a C API has to stop panics at the FFI boundary and propagate errors in a C-compatible way. It's not "catching" in the Java sense; nobody is using catch_panic to implement resumable error-handling.
Re:Making Rust binaries smaller by default by josephg on Wednesday January 24, 2024 @06:10AM (#39115977)
Oh no, I didn't know this. I'm adding panic = "abort" to all my projects. If I wanted exceptions I wouldn't be using rust.
Re:Making Rust binaries smaller by default by lifthrasiir on Wednesday January 24, 2024 @06:21AM (#39116024)
That's actually not too bad if you know you don't need to recover from panic. I kinda hope that `panic = "abort"` is indeed default unless overridden by dependencies, say, tokio...
Re:Making Rust binaries smaller by default by formerly_proven on Wednesday January 24, 2024 @06:33AM (#39116108)
Re:Making Rust binaries smaller by default by IshKebab on Wednesday January 24, 2024 @08:55AM (#39117373)
You aren't. They're only caught in special cases, e.g. if a thread panics and you want to propagate that to other threads, to catch panics in unit tests, to avoid unwinding across FFI, etc.

You shouldn't use them as a general exception mechanism. They aren't the same, even if under the hood they both use stack unwinding.

Re:Making Rust binaries smaller by default by vacuity on Wednesday January 24, 2024 @01:08PM (#39120606)
Naively, what is the difference between panics and exceptions? Is it the philosophy on when/how to use them?
Re:Making Rust binaries smaller by default by IshKebab on Thursday January 25, 2024 @11:57AM (#39131719)
Yes, and also exceptions can have arbitrary value associated with them. You aren't going to get far using panics as a general error handling mechanism because there's only one type of panic.