#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

#981 in Procedural macros

Download history 523/week @ 2024-11-17 425/week @ 2024-11-24 502/week @ 2024-12-01 1072/week @ 2024-12-08 1039/week @ 2024-12-15 144/week @ 2024-12-22 216/week @ 2024-12-29 1243/week @ 2025-01-05 1404/week @ 2025-01-12 1321/week @ 2025-01-19 691/week @ 2025-01-26 503/week @ 2025-02-02 576/week @ 2025-02-09 397/week @ 2025-02-16 904/week @ 2025-02-23 563/week @ 2025-03-02

2,489 downloads per month
Used in 20 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
~45K SLoC