使用assert_eq或打印大型固定大小的数组不起作用 [英] Using assert_eq or printing large fixed sized arrays doesn't work
本文介绍了使用assert_eq或打印大型固定大小的数组不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我编写了一些测试,需要断言两个数组相等.有些数组是[u8; 48]
,而另一些数组是[u8; 188]
:
I have written some tests where I need to assert that two arrays are equal. Some arrays are [u8; 48]
while others are [u8; 188]
:
#[test]
fn mul() {
let mut t1: [u8; 48] = [0; 48];
let t2: [u8; 48] = [0; 48];
// some computation goes here.
assert_eq!(t1, t2, "\nExpected\n{:?}\nfound\n{:?}", t2, t1);
}
我在这里遇到多个错误:
I get multiple errors here:
error[E0369]: binary operation `==` cannot be applied to type `[u8; 48]`
--> src/main.rs:8:5
|
8 | assert_eq!(t1, t2, "\nExpected\n{:?}\nfound\n{:?}", t2, t1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: an implementation of `std::cmp::PartialEq` might be missing for `[u8; 48]`
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
error[E0277]: the trait bound `[u8; 48]: std::fmt::Debug` is not satisfied
--> src/main.rs:8:57
|
8 | assert_eq!(t1, t2, "\nExpected\n{:?}\nfound\n{:?}", t2, t1);
| ^^ `[u8; 48]` cannot be formatted using `:?`; if it is defined in your crate, add `#[derive(Debug)]` or manually implement it
|
= help: the trait `std::fmt::Debug` is not implemented for `[u8; 48]`
= note: required by `std::fmt::Debug::fmt`
尝试将它们打印为像t2[..]
或t1[..]
这样的切片似乎无效.
Trying to print them as slices like t2[..]
or t1[..]
doesn't seem to work.
如何对这些数组使用assert
并打印它们?
How do I use assert
with these arrays and print them?
推荐答案
对于比较部分,您只需将数组转换为迭代器,然后逐元素进行比较即可.
For the comparison part you can just convert the arrays to iterators and compare elementwise.
assert_eq!(t1.len(), t2.len(), "Arrays don't have the same length");
assert!(t1.iter().zip(t2.iter()).all(|(a,b)| a == b), "Arrays are not equal");
这篇关于使用assert_eq或打印大型固定大小的数组不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文