jeudi 28 décembre 2017

Using assert with fixed sized arrays and printing them in Rust

I have written some tests, where at the end I need to assert that two arrays are equal. In some tests arrays have size and type of [u8; 48], whereas in others [u8; 188]. So all of them are u8 (byte) arrays. For example, one test:

#[test]
fn mul() {
    let mut t1: [u8; 48] = [248, 132, 131, 130, 138, 113, 205, 237, 20, 122, 66, 212, 191, 53, 59, 115, 56, 207, 215, 148, 207, 41, 130, 248, 214, 42, 124, 12, 153, 108, 197, 99, 199, 34, 66, 143, 126, 168, 88, 184, 245, 234, 37, 181, 198, 201, 84, 2];
    let t2: [u8; 48] = [232, 142, 138, 135, 159, 84, 104, 201, 62, 110, 199, 124, 63, 161, 177, 89, 169, 109, 135, 190, 110, 125, 134, 233, 132, 128, 116, 37, 203, 69, 80, 43, 86, 104, 198, 173, 123, 249, 9, 41, 225, 192, 113, 31, 84, 93, 254, 6];

    // some computation goes here.

    assert_eq!(t1, t2, "\nExpected\n{:?}\nfound\n{:?}", t2, t1);
}

But, I get multiple errors here, for one:

[u8; 48] cannot be formatted using :?

Trying to print them here as slices also like t2[..] or t1[..] doesn't seem to work. Another error is this:

binary operation == cannot be applied to type [u8; 48]

Any ideas how to use assert with these arrays and print them?

Aucun commentaire:

Enregistrer un commentaire