#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

#31 in #concat

Download history 32/week @ 2023-11-27 20/week @ 2023-12-04 30/week @ 2023-12-11 34/week @ 2023-12-18 23/week @ 2023-12-25 23/week @ 2024-01-01 24/week @ 2024-01-08 53/week @ 2024-01-15 36/week @ 2024-01-22 4/week @ 2024-01-29 29/week @ 2024-02-05 49/week @ 2024-02-12 40/week @ 2024-02-19 58/week @ 2024-02-26 55/week @ 2024-03-04 55/week @ 2024-03-11

216 downloads per month
Used in 10 crates (5 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

~1.5MB
~41K SLoC