为什么测试失败并显示消息 "panicked at Box<Any>"?

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

为什么会出现这种恐慌?

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.

大概很简单,就是没看懂

您正在使用 assert!。这期望第一个参数是布尔表达式。任何后续参数都被视为格式字符串和参数:

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

您可能想删除 assert! 的第二个参数或切换到 assert_eq!


我认为根本问题来自 bug (#30143),在某些情况下,该 bug (#30143) 允许将非格式字符串用作格式字符串。