Search code examples
rustpanic

Why does a test fail with the message "panicked at Box<Any>"?


Why does this panic?

pub fn testbool() -> bool {
    vec!['a', 'd', 'i', 'e', 'p', 'r']
        .iter()
        .enumerate()
        .find(|(_i, &c)| c != 'c')
        .is_none()
}

#[test]
fn test_testbool() {
    assert!(testbool(), true);
}

playground

---- test_testbool stdout ----
thread 'test_testbool' panicked at 'Box<Any>', src/lib.rs:11:5
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

It is probably very simple, but I don't understand it.


Solution

  • You are using assert!. This expects that the first argument is a boolean expression. Any subsequent arguments are considered a format string and arguments for that:

    assert!(testbool(), "Did not work: {}", 42);
    
    thread 'test_testbool' panicked at 'Did not work: 42'
    

    You probably want to remove the second argument to assert! or switch to assert_eq!.


    I believe the root issue comes from a bug (#30143) that allows non format strings to be used as a format string, in certain cases.