Le 01/02/2013 13:38, Lucian Branescu a écrit :
It's also possible to write something like python's enumerate, to get:
for enumerate(some_vector) Ii, e| { ... }
In general, rust loops are closer to Python's and functional map than
C++'s looping constructs.
Python’s enumerate() works on any iterable, not just lists. Is it
possible to chain rust for-loops to get something that works not just on
vectors?
The best I can think of (untested) is:
fn enumerate<T>(inner_loop: &fn(&fn(T) -> bool),
it: &fn(uint, T) -> bool) {
let i = 0u;
for inner_loop |el| {
if !it(i, el) { break }
i += 1;
}
}
for enumarate(|it| { some_str.each_char(it) }) |i, ch| { … }
… but it’s not pretty, and doesn’t work with eg. vec::each2 which gives
two arguments to its own `it`.
--
Simon Sapin
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev