-
-
Save yoshuawuyts/8d0e7946496d26b7d7e5b660643bf108 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