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 the use of any external library to be included in the program. If you want to zip two or three iterator value together then you can use zip() function for that. If we use zip() function in rust, it will always return us 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 use 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 zip() function here. It can be call on any iterator object in rust. Also we can pass multiple iterator inside it we want.
Example:
let demoitr = x.iter()
.zip(y.iter())
.zip(z.iter())
How zip Function Works in Rust?
As we already know now that zip() function is normal function like any other functions available in rust, zip function return us the new iterator which contain the zip of all the elements passed inside it in the form of tuple. After the successful creation of new iterator from the zip function we can print our values to understand the output and how it merges the multiple iterator together 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()
It give us the values in the form of tuple but these tuple are created by using several values from the different iterator that we are using with zip() function in rust. For instance, we have three iterators which are named as ite1, itr2 and itr3 and we want to merge this iterator together to form a new one iterator in rust.
Leta’s take 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 iterator in rust we must call zip function on them and internally it will pick first value from the first iterator that is ‘1’ in our case, on moving ahead it will take up first value from second iterator that is ’11’ in our case, followed by taking third iterator into consideration and will take up ’21’ from the third iterator hence in this way it will create the tuple from each values from the above mentioned iterator. On moving forward it will take up the second value from first iterator and son on, this will help it to create the tuple one by one using zip function in rust. These values should be in equal in number in order to form a tuple using zip.
Let’s take a look at its output what it will return us:
Output:
1, 11, 21
2, 12, 22
3, 13, 23
4, 14, 24
As you can see in the above output we have new iterator which contain new values, it is formed by using the several values from the different iterator we use with zip function in rust.
Return Type:
If we talk about its return type it will always return us new iterator which will contain the tuple formed by using the zip function and sequence of various iterator values.
Rules to keep in mind while working with zip function in rust:
- zip() will always return us new iterator object, if you want to see its value then we have to iterator it in order to see the tuples value formed by using the iterators 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.
- zip() function can be called in the iterator object ant inside this we can pass our iterator object that we want to zip with another iterator object in rust.
Example of Rust zip
Given below is the example mentioned:
In this example we are trying to use zip function with multiple iterator, also we have print the iterator values, which contain the new tuple from all three iterator values.
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 zip function in rust we can merge value of several iterators together to form a new iterator. We can still call the zip function on newly created iterator to zip it once again there is no restriction to use it once per iterator object. Also it is easy to use and readable by the developer to understand it clearly.
Recommended Articles
This is a guide to Rust zip. Here we discuss the introduction, how zip function works in rust? along with example respectively. You may also have a look at the following articles to learn more –
41 Online Courses | 13 Hands-on Projects | 322+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses