Le 11/08/2013 21:30, Tom Lee a écrit :
On Sun, Aug 11, 2013 at 1:27 PM, Patrick Walton <pwal...@mozilla.com
<mailto:pwal...@mozilla.com>> wrote:
On 8/12/13 8:25 AM, Tom Lee wrote:
:) I can appreciate that, but I'm still not really convinced
that this
"problem" deserves more syntax.
That said, I might hate for..else less if it used something
other than
the 'else' keyword. *shrug*
It seems to me that a big benefit of macros in Rust is to make
less-commonly-used syntax still usable and reasonably convenient
without having to build it into the compiler. "for/else" may be a
perfect use case here.
You're right. I keep forgetting the macro proposal when I come back to
read this thread.
Here it is, with an usage example. You have to assign the iterator to a
mutable variable explicitly:
macro_rules! for_(
($pattern: pat in $iter: ident $body: expr) => {
for_!($pattern in $iter $body else {});
};
($pattern: pat in $iter: ident $body: expr else $else_: expr) => {
loop {
match $iter.next() {
None => { $else_; break },
Some($pattern) => { $body; },
}
}
};
)
fn main() {
let v = ~[1u, 7, 42, 0];
let mut it = v.move_iter().enumerate();
for_!((i, v) in it {
if v == 42 {
printfln!("Found it at %u", i);
break
}
} else {
println("Not found");
})
}
--
Simon Sapin
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev