为什么这种恐慌?
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);
}
游乐场
---- 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!
。
我相信根部问题来自一个错误(#30143(,该错误允许在某些情况下用作格式字符串。