Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to iterate over a tuple?

I want to iterate over a tuple using a loop, like in Python. Is it possible in Rust?

let tup1 = (1, '2', 3.0);
for i in tup1.iter() {
    println!("{}", i);
}          
like image 388
Ahmed Saeed Avatar asked Aug 24 '19 19:08

Ahmed Saeed


People also ask

Can you iterate over a tuple?

We can iterate over tuples using a simple for-loop. We can do common sequence operations on tuples like indexing, slicing, concatenation, multiplication, getting the min, max value and so on.

What is traversing a tuple?

There are different ways to iterate through a tuple object. The for statement in Python has a variant which traverses a tuple till it is exhausted. It is equivalent to foreach statement in Java. Its syntax is − for var in tuple: stmt1 stmt2.

Can you splice a tuple?

To slice a Tuple in Python, use slice() builtin function. We can just provide stop position for slicing a tuple; or provide both start and stop positions to slice() function. slice() function returns indices. We use these indices along with the original tuple, to create the resulting sliced tuple.

Why can't I iterate over a tuple in Python?

Python is dynamically typed. 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.

How to iterate through a tuple in C++?

A C++ tuple is a container that can store multiple values of multiple types in it. We can access the elements of the tuple using std::get(), but std::get() always takes a constant variable parameter, so we can not simply iterate through it using a loop. For tasks that require iterating through all elements of the tuple. like printing all elements.

Why can’t I use a regular for loop with a tuple?

If you have a standard container, it’s easy to use a range-based for loop and iterate over its elements at runtime. How about std::tuple? In this case, we cannot use a regular loop as it doesn’t “understand” tuple’s compile-time list of arguments.

How to access a tuple’S element?

If you want to access a pair’s element you can just ask for .first or .second entry: On the other hand, since tuple has a variable size there’s no .first or .third … you can only access it through std::get: See at @Compiler Explorer. How to iterate?


1 Answers

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?
like image 103
Peter Hall Avatar answered Oct 22 '22 11:10

Peter Hall