Why are tuples not destroyed when repeating an array of tuples?

When repeating an array of tuples, why does Rust not destroy the tuples? For instance:

let x: &[(usize, usize)] = &[...]; for (a,b) in x.iter() { ... } 

leads to an error:

 error: type mismatch resolving `<core::slice::Iter<'_, (usize, usize)> as core::iter::Iterator>::Item == (_, _)`: expected &-ptr, found tuple [E0271] 
+5
source share
1 answer

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 typechecker complains fairly.

You can solve this problem by adding & to your template, for example:

 for &(a,b) in x.iter() { 
+13
source

Source: https://habr.com/ru/post/1215314/


All Articles