Skip to content

Instantly share code, notes, and snippets.

Created June 29, 2017 13:35
Show Gist options
  • Save anonymous/acf078d49bd8a82cc0b229e77b5cd904 to your computer and use it in GitHub Desktop.
Save anonymous/acf078d49bd8a82cc0b229e77b5cd904 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
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