Skip to content

Instantly share code, notes, and snippets.

@gyu-don
Created August 15, 2017 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gyu-don/c5c8186f1d359d30d0a1a8bda75eea7f to your computer and use it in GitHub Desktop.
Save gyu-don/c5c8186f1d359d30d0a1a8bda75eea7f to your computer and use it in GitHub Desktop.
Rust macro for print variable (or value)
macro_rules! print_var_fmt {
($e:expr) => {
concat!(stringify!($e), ":\t{}")
};
($e:expr, $($es: expr),+) => {
concat!(print_var_fmt!($e), ",\t", print_var_fmt!($($es),+))
};
}
macro_rules! print_var {
($($es:expr),+) => {
println!(print_var_fmt!($($es),*), $($es),*);
};
}
fn main() {
let x = 1;
print_var!(3);
print_var!(1, "2");
print_var!(x + 1, x - 1, x*2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment