#literals #string-literal #definition

macro macropol

Ergonomic string literal interpolation in macro definitions

4 releases

0.1.3 Apr 23, 2022
0.1.2 Dec 8, 2021
0.1.1 Dec 8, 2021
0.1.0 Dec 8, 2021

#691 in Procedural macros

Download history 276/week @ 2024-07-21 293/week @ 2024-07-28 200/week @ 2024-08-04 204/week @ 2024-08-11 173/week @ 2024-08-18 199/week @ 2024-08-25 155/week @ 2024-09-01 166/week @ 2024-09-08 186/week @ 2024-09-15 284/week @ 2024-09-22 256/week @ 2024-09-29 131/week @ 2024-10-06 822/week @ 2024-10-13 634/week @ 2024-10-20 901/week @ 2024-10-27 524/week @ 2024-11-03

2,901 downloads per month
Used in 18 crates (7 directly)

MIT/Apache

12KB
229 lines

🚨 macropol

Ergonomic string literal interpolation in macro definitions.

Replaces metavariables ($foo) and arbitrary expressions in string literals (including doc comments) and concatenates them with surrounding text fragments with core::concat!.

#[macropol::macropol]
macro_rules! mymacro {
    ($count:expr, $name:expr, fn $func:ident()) => {
        /// Returns `"$$ $name, ${stringify!($count)} to beam up"`.
        fn $func() -> &'static str {
            "$$ $name, $&count to beam up"
        }
    };
}

// The above definition expands to:
//
//     macro_rules! mymacro {
//         ($count:expr, $name:expr, fn $func:ident()) => {
//             #[doc = concat!("Returns `\"$ ", $name, ", ",
//                  stringify!($count), " to beam up\"`.")]
//             fn func() -> &'static str {
//                 concat!("$ ", $name, ", ",
//                     stringify!($count), " to beam up")
//             }
//         };
//     }
//

mymacro!(3, "Scotty", fn func());

assert_eq!(func(), "$ Scotty, 3 to beam up");

Dependencies

~2MB
~44K SLoC