Compare vec to some fixed arrays

I'm trying to compare a rotation matrix from a file to a few knows rotation matrices, like this:

``````struct RotationMatrix;
impl RotationMatrix {
const NO_ROTATION: [u8; 36] = [
0, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x40, 0, 0, 0,
];
const ROTATION_90: [u8; 36] = [
0, 0, 0, 0, 0, 0x01, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x40, 0,
0, 0,
];
const ROTATION_180: [u8; 36] = [
0xFF, 0xFF, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x40,
0, 0, 0,
];
const ROTATION_270: [u8; 36] = [
0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0, 0, 0, 0, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x40, 0,
0, 0,
];
}
``````
``````let matrix_structure: Vec<u8> = file.read_to_end()?;
match data_from_file {
RotationMatrix::NO_ROTATION => { println!("No rotation") },
_ => println!("Some rotation"),
}
``````

I get this:

``````  --> src\rotate.rs:77:3
|
9  |       const NO_ROTATION: [u8; 36] = [
|  _____-
10 | |         0, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x40, 0, 0, 0,
11 | |     ];
| |______- associated constant defined here
...
76 |       match matrix_structure {
|             ---------------- this expression has type `Vec<u8>`
77 |           RotationMatrix::NO_ROTATION => println!("No rotation"),
|           ^^^^^^^^^^^^^^^^-----------
|           |               |
|           |               help: introduce a new binding instead: `other_no_rotation`
|           expected struct `Vec`, found array `[u8; 36]`
|           `NO_ROTATION` is interpreted as an associated constant, not a new binding
|
= note: expected type `Vec<u8>`
found array `[u8; 36]`
``````

How can I make this work? Feel free to store the known rotation matrices in a different type, this was just the first idea I came up with.

Try `match &*matrix_structure { RotationMatrix::NO_ROTATION => ...` when using `const NO_ROTATION: &[u8] = &[0, 0x01, ...`.

Makes no difference. (And shouldn't because of match ergonomics?)

This compiles:

``````struct RotationMatrix;
impl RotationMatrix {
const NO_ROTATION: &'static [u8] = &[
0, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x40, 0, 0, 0,
];
}

fn main() {
let matrix_structure: Vec<u8> = todo!();
match &*matrix_structure {
RotationMatrix::NO_ROTATION => { println!("No rotation") },
_ => println!("Some rotation"),
}
}
``````

Oh, you changed the type at the top as well, didn't see that.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.