Skip to content

Instantly share code, notes, and snippets.

@viperscape
Created February 25, 2015 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viperscape/b5c002ce9402a86dbdc3 to your computer and use it in GitHub Desktop.
Save viperscape/b5c002ce9402a86dbdc3 to your computer and use it in GitHub Desktop.
range in rust, without the need to think about range direction
#![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