When iterating over an array of tuples, why does Rust not destructure the tuples? For example:
let x: &[(usize, usize)] = &[...];
for (a,b) in x.iter() {
...
}
leads to the error:
error: type mismatch resolving `<core::slice::Iter<'_, (usize, usize)> as core::iter::Iterator>::Item == (_, _)`:
expected &-ptr,
found tuple [E0271]
The problem is that your pattern (a, b)
is a tuple of type (usize, usize)
, while your iterator returns references to tuples (i.e. &(usize, usize)
), so the typechecker rightly complains.
You can solve this by adding an &
in your pattern, like this:
for &(a,b) in x.iter() {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With