Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do iterators return a reference to items or the value of the items in Rust?

Tags:

loops

rust

If I have a vector:

let mut v = vec![0, 1, 2, 3, 4, 5];

And I iterate through it:

for &item in v.iter() {}

Would &item here be a reference or a value? It seems like it would be a reference from the & part, but reading the details seem to show it's a value???

like image 529
Bill Avatar asked Mar 20 '20 19:03

Bill


People also ask

How do iterators work in Rust?

In Rust, iterators are lazy, meaning they have no effect until you call methods that consume the iterator to use it up. For example, the code in Listing 13-10 creates an iterator over the items in the vector v1 by calling the iter method defined on Vec<T> . This code by itself doesn't do anything useful.

Is iterator empty Rust?

You can make your iterator peekable and peek the first item; if it's None , then the iterator is empty.

Are Rust iterators zero cost?

Iterators are one of Rust's zero-cost abstractions, by which we mean using the abstraction imposes no additional runtime overhead.

Why are iterators useful?

The primary purpose of an iterator is to allow a user to process every element of a container while isolating the user from the internal structure of the container. This allows the container to store elements in any manner it wishes while allowing the user to treat it as if it were a simple sequence or list.


1 Answers

Do iterators return a reference to items or the value of the items in Rust?

There's no general answer to this question. An iterator can return either. You can find the type of the items by looking up the associated type Iterator::Item in the documentation. The documentation of Vec::iter(), for example, tells you that the return type is std::slice::Iter. The documentation of Iter in turn has a list of the traits the type implements, and the Iterator trait is one of them. If you expand the documentation, you can see

type Item = &'a T

which tells you that the item type for the iterator return by Vec<T>::iter() it &T, i.e. you get references to the item type of the vector itself.

In the notation

for &item in v.iter() {}

the part after for is a pattern that is matched against the items in the iterator. In the first iteration &item is matched against &0, so item becomes 0. You can read more about pattern matching in any Rust introduction.

Another way to iterate over the vector v is to write

for item in v {}

This will consume the vector, so it can't be used anymore after the loop. All items are moved out of the vector and returned by value. This uses the IntoIterator trait implemented for Vec<T>, so look it up in the documentation to find its item type!

The first loop above is usually written as

for &item in &v {}

which borrows v as a reference &Vec<i32>, and then calls IntoIterator on that reference, which will return the same Iter type mentioned above, so it will also yield references.

like image 181
Sven Marnach Avatar answered Sep 29 '22 11:09

Sven Marnach