vec![] / assert!() / assert_eq!() / dbg!()
Rust provides utility macros for common tasks: vec![] for easily creating a Vec, assert!() and assert_eq!() for verifying program correctness, and dbg!() for printing debug values.
Syntax
// vec![] — Initializes a Vec.
let v = vec![1, 2, 3];
let zeros = vec![0; 5]; // [0, 0, 0, 0, 0]
// assert!() — Panics if the condition is false.
assert!(condition);
assert!(condition, "Error message: {}", value);
// assert_eq!() — Panics if the two values are not equal.
assert_eq!(left, right);
assert_eq!(left, right, "message");
// assert_ne!() — Verifies that two values are not equal.
assert_ne!(left, right);
// dbg!() — Prints the value to stderr and returns it (ownership is returned).
let x = dbg!(5 + 3); // x becomes 8.
dbg!(&v); // Passes by reference to retain ownership.
// todo!() / unimplemented!() / unreachable!()
fn todo_fn() -> i32 { todo!() }
fn unimpl() -> i32 { unimplemented!() }
Macro List
| Macro | Description |
|---|---|
| vec![a, b, c] | Creates a Vec with the specified initial values. |
| vec![val; n] | Creates a Vec containing n copies of the same value. |
| assert!(cond) | Panics if the condition is false. Used for tests and invariant checks. |
| assert_eq!(a, b) | Panics if a == b is false. Displays both values on failure. |
| assert_ne!(a, b) | Panics if a != b is false. |
| debug_assert!() | An assert that is only evaluated in debug builds (disabled in release builds). |
| dbg!(expr) | Evaluates the expression, prints the file, line, and value to stderr, then returns the value. |
| todo!() | Panics to mark unfinished code. Explicitly indicates "not implemented yet." |
| unimplemented!() | Panics to explicitly indicate that something will not be implemented. |
| unreachable!() | Panics to indicate that a code path should never be reached. |
Sample Code
fn add(a: i32, b: i32) -> i32 { a + b }
fn divide(a: f64, b: f64) -> f64 {
assert!(b != 0.0, "Cannot divide by zero");
a / b
}
fn main() {
// Create a Vec using the vec![] macro.
let nums = vec![10, 20, 30, 40, 50];
let zeros = vec![0u8; 3]; // [0, 0, 0]
println!("nums: {:?}", nums);
println!("zeros: {:?}", zeros);
// assert!() — Check a precondition.
assert!(nums.len() == 5, "Expected length to be 5");
println!("assert! passed");
// assert_eq!() — Verify a calculation result.
assert_eq!(add(2, 3), 5);
assert_eq!(add(2, 3), 5, "2 + 3 should equal 5");
println!("assert_eq! passed");
// assert_ne!() — Verify that two values differ.
assert_ne!(add(2, 3), 6);
println!("assert_ne! passed");
// dbg!() — Debug output (returns the value).
let x = dbg!(2 + 3); // stderr: [src/main.rs:X] 2 + 3 = 5
let y = dbg!(x * 2); // stderr: [src/main.rs:X] x * 2 = 10
println!("x={x}, y={y}"); // 5, 10
// Passing by reference to dbg!() retains ownership.
let v = vec![1, 2, 3];
dbg!(&v);
println!("v is still usable: {:?}", v);
// Chaining dbg!() calls.
let result = dbg!(nums.iter().sum::<i32>());
println!("Sum: {result}");
// assert! in divide — normal case.
let r = divide(10.0, 3.0);
println!("10 / 3 = {:.4}", r);
// #[cfg(debug_assertions)] — Runs only in debug builds.
debug_assert!(1 + 1 == 2, "Math is broken");
println!("debug_assert! passed (debug builds only)");
}
// Using todo!() — compiles successfully but panics at runtime.
#[allow(dead_code)]
fn not_yet_implemented() -> i32 {
todo!("To be implemented later")
}
Notes
Use assert!() and assert_eq!() for tests and invariant checks. Because they panic on failure, do not use them for error handling in production code — reserve them for conditions that must always hold. In performance-critical sections, use debug_assert!() so the check is disabled in release builds.
The dbg!() macro is very handy for inspecting values during debugging. You can wrap an expression in place and it returns the value, so you can insert it without restructuring your code. Just remember to remove it after debugging.
For creating Vecs, see Vec::new() / vec![]. For details on panics, see panic!() / unwrap() / expect() — When to Use Each.
If you find any errors or copyright issues, please contact us.