Created
August 15, 2017 22:59
-
-
Save gyu-don/c5c8186f1d359d30d0a1a8bda75eea7f to your computer and use it in GitHub Desktop.
Rust macro for print variable (or value)
This file contains hidden or 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! 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