How to Use assert!, assert_eq!, and assert_ne! in Rust

Use `assert!` to verify a condition is true, `assert_eq!` to check two values are equal, and `assert_ne!` to ensure they are different; all three panic if the check fails. Place these macros inside test functions annotated with `#[test]` or directly in `main` to validate logic immediately.

How to Use assert!, assert_eq!, and assert_ne! in Rust

Use assert! to verify a condition is true, assert_eq! to check two values are equal, and assert_ne! to ensure they are different; all three panic if the check fails. Place these macros inside test functions annotated with #[test] or directly in main to validate logic immediately.

pub fn add(left: u64, right: u64) -> u64 {
    left + right
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_add() {
        let result = add(2, 2);
        assert_eq!(result, 4);
        assert_ne!(result, 5);
        assert!(result > 0);
    }
}