Answers for "assert_eq rust"

1

assert_eq rust

// The assert_eq! macro is used to check if 2 expressions are equal. 
// source: https://doc.rust-lang.org/std/macro.assert_eq.html
// This macro is commonly used in testing. See more: https://doc.rust-lang.org/book/ch11-01-writing-tests.html
// For example;

let a = 3;
let b = 1 + 2;
assert_eq!(a, b);

assert_eq!(a, b, "we are testing addition with {} and {}", a, b);


// There is also the assert_ne! macro to check if 2 expressions are not equal. 
// source: https://doc.rust-lang.org/std/macro.assert_ne.html
// For example;

let a = 3;
let b = 2;
assert_ne!(a, b);

assert_ne!(a, b, "we are testing that the values are not equal");
Posted by: Guest on March-06-2022

Browse Popular Code Answers by Language