Skip to content

Instantly share code, notes, and snippets.

@BookOwl
Forked from anonymous/playground.rs
Last active June 29, 2017 13:35
Show Gist options
  • Save BookOwl/3498074f1a3f4b49c1384cbaa3f24ce0 to your computer and use it in GitHub Desktop.
Save BookOwl/3498074f1a3f4b49c1384cbaa3f24ce0 to your computer and use it in GitHub Desktop.
A hacky sequence macro for emulating C-style for loops
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