Re:Effective Rust
by jstimpfle on Thursday June 15, 2023 @11:37AM
(#36342055)
As an outsider, is panic() like assert()?
[ Parent
| on HN ]
It is like printf to stderr and exit(1)
Re:Effective Rust
by bluejekyll on Thursday June 15, 2023 @11:47AM
(#36342204)
assertions often get turned off in production/release code, panics do not.
Not in Rust. You can use debug_assert if you don’t want to assert in prod tho
Re:Effective Rust
by bluejekyll on Thursday June 15, 2023 @03:16PM
(#36345198)
I meant generally, you are absolutely correct about rust.
assert!(cond) in rust is basically
if !cond {
// I ad-libbed the exact error message
panic!("Asssertion failed {cond} was false, line/file information, etc")
}
panic!(message) is "either throw an exception containing message, or print the message + maybe a backtrace and abort the process".You can use compiler flags to guarantee that it aborts instead of throwing an exception, you can't use compiler flags to guarantee that it acts an exception, sometimes rust will just abort the process (for example if you panic!() while you're already unwinding because of a prior panic!()).
If nothing catches the exception (and that's usually the case, by convention), the runtime will print the message, maybe a backtrace, and kill the thread that paniced.