Created
February 25, 2015 19:18
-
-
Save viperscape/b5c002ce9402a86dbdc3 to your computer and use it in GitHub Desktop.
range in rust, without the need to think about range direction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(core)] | |
extern crate core; | |
use self::core::num::Int; | |
fn rng<T:PartialEq+Int, F:FnMut(T)> (d:T,dt:T,mut f:F) { | |
if d>dt { for n in (dt..d).rev() { f(n); } } | |
else { for n in (d..dt) { f(n); } } | |
} | |
fn main () { | |
rng (5,0, |n| println!("{:?}",n)); | |
} | |
/* | |
4 | |
3 | |
2 | |
1 | |
0 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment