Skip to content

Instantly share code, notes, and snippets.

@tommie
Created July 15, 2014 20:45
Show Gist options
  • Save tommie/71408da282761c237aa9 to your computer and use it in GitHub Desktop.
Save tommie/71408da282761c237aa9 to your computer and use it in GitHub Desktop.
#![feature(macro_rules)]
macro_rules! cfor(
($($ident:ident = $pre:expr),*; $cond:expr; $($post:expr),*; $block:block) => ({
$( let mut $ident = $pre; )*
while $cond {
$block
$( $post; )*
}
});
)
fn main() {
cfor!(i = 0i, j = 1i; i < 5; i += 1, j += 2; {
println!("Hello {} {}", i, j);
})
}
@madeinquant
Copy link

madeinquant commented Jun 18, 2018

Thank for your c loop in rust, it is very useful for me. To declare multiple mutable variables at the same time. Defined a macro to declare mutable variables as the following.

macro_rules! double {
    ( $($var:ident),+) => {
        $(let mut $var: f64;)+
    };
}

fn main() {
    double!(FT, FX, alpha, H, K, lambda, T, X);

    FT = 2.0;
    println!("{}", FT);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment