Updated June 2, 2023
Introduction to Rust zip
As the name suggest it is used to zip up the values of two different iterators in rust. This is an in built function available in Rust so we can use it directly without any external library to be included in the program. If you want to zip two or three iterator values together,n you can use the zip() function. Suppose we use the zip() function in Rust. In that case, it will always return a new iterator containing a tuple which will hole the value from both the iterator in sequence and keep on iterating until the last element is reached.
Syntax:
As seen, we used the zip() function to zip the multiple iterator values together, and it formed a sequence of tuple for us.
let variable_name = iterator_name1.iter()
.zip(b.iterator_name2())
.zip(c.iterator_name1()) .. and so on ..
As you can see in the above lines of syntax, we are using the zip() function here. It can be called on any iterator object in Rust. Also, we can pass multiple iterators inside if we want.
Example:
let demoitr = x.iter()
.zip(y.iter())
.zip(z.iter())
How Zip Function Works in Rust?
As we already know now, the zip() function is a normal function like any other function available in Rust, zip function returns us the new iterator, which contains the zip of all the elements passed inside it in the form of a tuple. After successfully creating a new iterator from the zip function, we can print our values to understand the output and how it merges the multiple iterators to form a new one in Rust.
Let’s take a look at its internal working followed by the sample example to distract the usage in rust:
zip()
The zip() function in Rust allows us to merge multiple iterators named ite1, itr2, and itr3, forming a new iterator that provides values in the form of tuples created from values of these different iterators.
Let’s take a dummy value for more visualization:
Code:
itr1 = [1, 2, 3, 4]
itr2 = [11, 12, 13 , 14]
itr3 = [21, 22, 23, 24]
So if we try to zip these three different iterators in Rust, we must call the zip function on them, and internally it will pick the first value from the first iterator, that is ‘1’ in our case, on moving ahead, it will take up first value from a second iterator that is ’11’ in our case, followed by considering third iterator and will take up ’21’ from the third iterator hence in this way it will create the tuple from each value from the above-mentioned iterator. Moving forward, it will take up the second value from the first iterator, and so on; this will help it to create the tuple one by one using the zip function in Rust. These values should be equal in number to form a tuple using zip.
Let’s take a look at its output and what it will return us:
Output:
1, 11, 21
2, 12, 22
3, 13, 23
4, 14, 24
As shown in the above output, we have a new iterator that combines values from different iterators using the zip function in Rust.
Return Type:
The return type of the zip function is always a new iterator that contains tuples formed by combining values from the sequence of various iterators.
Rules to keep in mind while working with the zip function in rust:
- zip() will always return us a new iterator object, if you want to see its value, then we have to iterator it to know the value of the tuple formed by using the iterator values.
- It is an in build function of rust language, so we do not require to include any dependency in our configuration to use it.
- To use the zip() function in Rust, you can call it on an iterator object and pass another iterator object that you want to zip together.
Example of Rust zip
Given below is the example mentioned:
In this example, we use the zip function with multiple iterators and print the values of the resulting iterator, which contains tuples from all three iterators.
Code:
fn main(){
println!("Demo to show zip function working in rust programming !!!");
let itr1 = [100, 200, 300, 400, 500, 600];
let itr2 = [10 , 20, 30, 40, 50 ,60];
let itr3 = [1, 2, 3, 4 ,5 , 6];
let iter = itr1.iter()
.zip( itr2.iter())
.zip( itr3.iter())
.map(|((x, y), z)| (x, y, z));
println!("Printing the iterator object !!!!");
println!("{:?}", iter);
println!("Now we are trying to see the tuple values inside the newly created iterator object using zip in rust !! ::");
for (itr1, itr2, itr3) in iter {
println!("{} {} {}", itr1, itr2, itr3);
}
}
Output:
Conclusion
By using the zip function in rust, we can merge value of several iterators together to form a new iterator. You can call the zip function on a newly created iterator multiple times without any restrictions. Also, it is easy to use and readable by the developer to understand it clearly.
Recommended Articles
We hope that this EDUCBA information on “Rust zip” was beneficial to you. You can view EDUCBA’s recommended articles for more information.