Created
September 3, 2020 14:29
-
-
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
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! 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