-
-
Save BookOwl/3498074f1a3f4b49c1384cbaa3f24ce0 to your computer and use it in GitHub Desktop.
A hacky sequence macro for emulating C-style for loops
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
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