Is it possible to iterate over a tuple?

The type of each element of a tuple can be different, so you can’t iterate over them. Tuples are not even guaranteed to store their data in the same order as the type definition, so they wouldn’t be good candidates for efficient iteration, even if you were to implement Iterator for them yourself.

However, an array is exactly equivalent to a tuple, with all elements of the same type:

let tup = [1, 2, 3];
for i in tup.iter() {
    println!("{}", i);
}

See also:

  • How to iterate or map over tuples?
  • Why does the 2-tuple Functor instance only apply the function to the second element?

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)