Skip to content

Instantly share code, notes, and snippets.

@egordm
Created September 3, 2020 14:29
Show Gist options
  • Save egordm/de9ffbda5863a51605453a23db435009 to your computer and use it in GitHub Desktop.
Save egordm/de9ffbda5863a51605453a23db435009 to your computer and use it in GitHub Desktop.
Rust macro to format numbers into a given string. Numbers are are ascending accoriding to given tokens
macro_rules! format_numeric {
(($s: expr; $i: expr, $x: ident) -> ($($body:tt)*)) => {format!($s, $($body)* $i)};
(($s: expr; $i: expr, $x: ident, $($y: ident),+) -> ($($body:tt)*)) => {format_numeric!(($s; $i + 1, $($y),+) -> ($($body)* $i,))};
[$s: expr; $($y: ident),+] => { format_numeric!(($s; 1, $($y),+) -> ()) };
}
assert_eq!(
format_numeric!["{}, {} and {}"; a, b, c],
"1, 2 and 3"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment