[rust-dev] error: unresolved name: each

Niko Matsakis niko at alum.mit.edu
Mon Aug 20 10:40:32 PDT 2012


On 8/18/12 5:21 AM, Nadav Vinik wrote:
> for each loop don't work in 3.0 and trunk:
>
> for  each(~[2,4,8,5,16]) |n| {
>      if  n  %2  !=0  {
>          println(~"found odd number!");
>          break;
>      }
> }

What's missing here is a

     import vec::each;

however this would be more idiomatically written as

     for the_vector.each |n| { ... }

> also it is possible to get the value from this loop:
> import io::println;
>
> fn main() {
>      for 5.times {
>          println("Here's some Rust!");
>      }
> }

The preferred way (at least, my preferred way) is to use `uint::range` 
or `int::range`, depending on whether you want unsigned or signed 
integers.  For example:

     for uint::range(0, 5) |i| { ... }

This may change as we revamp our APIs.  At minimum, the `uint` prefix 
will probably go away and range will be replaced by a generic function 
that can iterate over any "integer-like" thing.


Niko


More information about the Rust-dev mailing list