#string-literal #literals #definition #interpolation #macro #concat #expression

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

#27 in #concat

Download history 153/week @ 2024-06-28 73/week @ 2024-07-05 215/week @ 2024-07-12 252/week @ 2024-07-19 335/week @ 2024-07-26 192/week @ 2024-08-02 231/week @ 2024-08-09 182/week @ 2024-08-16 166/week @ 2024-08-23 176/week @ 2024-08-30 160/week @ 2024-09-06 178/week @ 2024-09-13 288/week @ 2024-09-20 183/week @ 2024-09-27 205/week @ 2024-10-04 762/week @ 2024-10-11

1,462 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
~43K SLoC