See the section of the tutorial on for and do - they are desugared to use 
functions of that sort (i.e., the body is a closure which is the last parameter 
of the function used).

The following code compiles:

fn main() {
    for 5.timesi |i| {
        if i % 2 == 0 {
            loop;
        }
        io::println(i.to_str());
    }

    for int::range(0,10) |i| {
        io::println(i.to_str());
        if i > 5 {
            break;
        }
    }
}

And produces:

1
3
0
1
2
3
4
5
6

On Oct 24, 2012, at 1:28 PM, Dave Halperin wrote:

> Not exactly, those don't have the right signature to be used with a for loop, 
> they just take a function.  You wouldn't be able to use break or continue 
> with them.
> 
> On Wed, Oct 24, 2012 at 12:20 PM, Daniel Patterson <[email protected]> wrote:
> See times, timesi (implemented for uint and int), and int::range (i.e., these 
> all already exist)
> 
> On Oct 24, 2012, at 11:23 AM, Dave Halperin wrote:
> 
>> Python doesn't have c style for loops and the way you'd do this is use 
>> xrange to create an iterator over a range of numbers, then use a high level 
>> for loop.  This seems like the cleanest solution for rust to me.  
>> Psuedo-code:
>> 
>> for range(start, end) |i| {
>>   char c = buf[i];
>>   ...
>>   if (c == uninteresting) {
>>     continue;
>>   }
>>   ...
>> }
>> 
>> Seems like range and some related functions should be considered for the 
>> standard library to support this style.
>> 
>> On Wed, Oct 24, 2012 at 8:58 AM, Chris Double <[email protected]> 
>> wrote:
>> On Wed, Oct 24, 2012 at 8:19 PM, Henri Sivonen <[email protected]> wrote:
>> > Looping over a part of an array by index and moving on immediately
>> > when a “not interested” condition matches.
>> >
>> > Stuff like
>> > for (int i = start; i < end; i++) {
>> >   char c = buf[i];
>> >   ...
>> >   if (c == uninteresting) {
>> >     continue;
>> >   }
>> >   ...
>> > }
>> 
>> You might be able to bend macros into something you want. For example:
>> 
>> macro_rules! my_loop(
>>   ($cond:expr, $inc:expr, $body:expr) => {
>>     while $cond {
>>       while $cond {
>>         $body;
>>         $inc;
>>       }
>>       $inc;
>>     }
>>   };
>> )
>> 
>> fn main () {
>>   io::println("hello");
>>   let mut i = 0;
>>   my_loop!(i < 10, i += 1, {
>>     if i < 5 { break; }
>>     io::println("foo");
>>   })
>> }
>> 
>> Here 'break' inside the macro is your 'continue' and in the example "i
>> < 5" is the uninteresting check. I don't know how, in rust, to change
>> all uses of some_string into "($inc; loop)" but if you can you can do
>> better than this example.
>> 
>> Chris.
>> --
>> http://www.bluishcoder.co.nz
>> _______________________________________________
>> Rust-dev mailing list
>> [email protected]
>> https://mail.mozilla.org/listinfo/rust-dev
>> 
>> _______________________________________________
>> Rust-dev mailing list
>> [email protected]
>> https://mail.mozilla.org/listinfo/rust-dev
> 
> 

_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to