Verify error cases in Rust by marking test functions with #[should_panic] to assert that specific inputs cause a crash.
Use the #[should_panic] attribute on your test function to verify that a specific error case causes a panic.
#[cfg(test)]
mod tests {
#[test]
#[should_panic]
fn test_division_by_zero() {
let result = 10 / 0;
}
}
Run the test with cargo test to confirm the panic occurs as expected.
Testing error cases in Rust tells the compiler to expect your code to crash in a specific way. It's like checking that a safety alarm goes off when you break a rule. You use this when you want to prove your code correctly stops execution on impossible or dangerous inputs.