Converting a Rust Vector into an Iterator

Vectors can also be converted into iterators just like sets. The syntax to converts a vector into an iterator is:

let v_iter = v.into_iter();

Example:

fn main() {
	let v = Vec::from([2,4,8,10]);

	let v_iter = v.into_iter();

	for item in v_iter {
		println!("{}", item);
	}
}

An iterator can be converted back into a vector using the collect() method, we need to specify the type of the vector we want to convert it into.

let v: Vec<i32> = my_iter.collect(); // turns the iterator into a vector
let v: HashSet<i32> = my_other_iter.collect(); // turns iter into a set