Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created August 26, 2019 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rust-play/46e98608165d29acaf8c7c89eef1a616 to your computer and use it in GitHub Desktop.
Save rust-play/46e98608165d29acaf8c7c89eef1a616 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
#[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