3

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.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
VsM
  • 710
  • 1
  • 10
  • 23

1 Answers1

8

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.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366