Created
August 26, 2019 16:56
-
-
Save rust-play/46e98608165d29acaf8c7c89eef1a616 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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_export] | |
macro_rules! info { | |
// info!("...") | |
($e:expr) => { | |
$crate::info_impl!(($e)); | |
}; | |
// info!("...", args...) | |
($e:expr, $($rest:tt)*) => { | |
$crate::info_impl!(($e) $($rest)*); | |
}; | |
} | |
#[macro_export] | |
#[doc(hidden)] | |
macro_rules! info_impl { | |
// End of macro input | |
(($($e:expr),*)) => { | |
println!("msg={:?}", format!($($e),*)); | |
}; | |
// Trailing k-v pairs containing no trailing comma | |
(($($e:expr),*) { $($key:ident : $value:expr),* }) => { | |
println!("msg={:?}", format!($($e),*)); | |
$( | |
println!(" {}={:?}", stringify!($key), $value); | |
)* | |
}; | |
// Trailing k-v pairs with trailing comma | |
(($($e:expr),*) { $($key:ident : $value:expr,)* }) => { | |
$crate::info_impl!(($($e),*) { $($key : $value),* }); | |
}; | |
// Last expression arg with no trailing comma | |
(($($e:expr),*) $arg:expr) => { | |
$crate::info_impl!(($($e,)* $arg)); | |
}; | |
// Expression arg | |
(($($e:expr),*) $arg:expr, $($rest:tt)*) => { | |
$crate::info_impl!(($($e,)* $arg) $($rest)*); | |
}; | |
} | |
fn main() { | |
info!("hello"); | |
info!("hello",); | |
info!("hello {}", "cats"); | |
info!("hello {}", "cats",); | |
info!("hello {}", "cats", { | |
cat_1: "chashu", | |
cat_2: "nori", | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment