Practical Rust
Assertions
Checking a Function
Assertions verify expected results. Real test functions use #[test] with cargo test; this example checks inline with assert_eq!.
Program
Play the program to verify a double function and confirm all checks pass.
unit_tests.rs
fn double(n: i32) -> i32 {
n * 2
}
fn main() {
assert_eq!(double(2), 4);
assert_eq!(double(0), 0);
println!("all checks passed");
}
assert_eq!
`assert_eq!(a, b)` panics if the values differ.
function under test
`double` is the code being checked.
cargo test
Real tests live in `#[test]` functions run by `cargo test`.