Q:

Rust program to use an iterator to traverse the mutable vector elements

belongs to collection: Rust Iterator and Closure Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to use an iterator to traverse the mutable 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 mutable vector elements

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

Output:

Country names: 
  India
  UK
  USA
  Canada

Explanation:

Here, we created a mutable vector that contains the name of countries. Then we traversed the vector elements using the iter_mut() function and printed them.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Rust program to create a closure function with a p... >>
<< Rust program to use an iterator to traverse the ve...