4 ms·
As an outsider, is panic() like assert()?
by jstimpfle 3y ago
As an outsider, is panic() like assert()?
- PuercoPop 3y agoIt is like printf to stderr and exit(1)
- bluejekyll 3y agoassertions often get turned off in production/release code, panics do not.
- baby 3y agoNot in Rust. You can use debug_assert if you don’t want to assert in prod tho
- bluejekyll 3y agoI meant generally, you are absolutely correct about rust.
- deleted 3y ago[deleted]
- gpm 3y agoassert!(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.