This is the behaviour I tried:
struct Matrix(f32, f32, f32, f32);
let matrix = Matrix(1.1, 1.2, 2.1, 2.2);
matrix.len(); // expected `4`
which produces the error:
error[E0599]: no method named `len` found for type `&tuples::Matrix` in the current scope
--> src/tuples.rs:19:44
|
19 | println!("matrix length: {}", self.len());
| ^^^
|
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following traits define an item `len`, perhaps you need to implement one of them:
candidate #1: `std::iter::ExactSizeIterator`
candidate #2: `core::slice::SliceExt`
candidate #3: `core::str::StrExt`
std::iter::ExactSizeIterator
looks like a good candidate, but I still don't know how to implement it
Context
Whilst trying to reverse Matrix
, I realized that instead of dryly listing the reverse indexes of the matrix like so:
fn reverse(matrix: Matrix) -> Matrix {
return Matrix(matrix.3, matrix.2, matrix.1, matrix.0)
}
I could perhaps iterate over the Matrix
in reverse order. I saw How to iterate or map over tuples? and thought that it was complex. If one were able to get the length of the tuple, one could solve the question "How to iterate or map over tuples?" with a simpler solution. Obviously, I could just use '4' as the length, but what if I weren't using a struct but rather a tuple of an unknown length.