Created
June 29, 2017 13:35
-
-
Save anonymous/acf078d49bd8a82cc0b229e77b5cd904 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
This file contains hidden or 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
macro_rules! sequence { | |
($x:ident : $t:ty = $init:expr ; $cond:expr ; $change:expr) => {{ | |
struct Sequence { | |
val: $t, | |
} | |
impl Iterator for Sequence { | |
type Item = $t; | |
#[inline] | |
fn next(&mut self) -> Option<$t> { | |
let $x = self.val; | |
if $cond { | |
let old = self.val.clone(); | |
let $x = self.val.clone(); | |
self.val = $change; | |
Some(old) | |
} else { | |
None | |
} | |
} | |
} | |
Sequence{val: $init} | |
}} | |
} | |
fn main() { | |
for x in sequence!(x:i32 = 0; x < 10; x + 1) { | |
println!("{}", x); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment