ADVERTISEMENT
ADVERTISEMENT

Rust program to use an iterator to traverse the vector elements

Rust | Iterator and Closure Example: Write a program to demonstrate the use of an iterator to traverse the vector elements.
Submitted by Nidhi, on November 22, 2021

Problem Solution:

In this program, we will create a vector that contains the name of countries. Then we will traverse vector elements using iterator.

Program/Source Code:

The source code to use an iterator to traverse the vector elements is given below. The given program is compiled and executed on UBUNTU 18.04 successfully.

// Rust program to use of an iterator to 
// traverse the vector elements

fn main() 
{
   	let countries = vec!["India", "UK", "USA", "Canada"];
   
   	println!("Country names: ");
   	for country in countries.into_iter() 
   	{
		println!("  {}",country);
   	}
}

Output:

Country names: 
  India
  UK
  USA
  Canada

Explanation:

Here, we created a vector that contains the name of countries. Then we traversed the vector elements and printed them.

Rust Iterator and Closure Programs »



ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.