Separate declaration of type using macro_rules!

⚓ Rust    📅 2025-07-10    👤 surdeus    👁️ 5      

surdeus

How can I correctly declare type inside declarative macro by separating type's name itself and it's generics ?

Consider this example:

struct RectangleA<const WIDTH: usize, const LENGTH: usise>;
struct RectangleB<const WIDTH: usize, const LENGTH: usise>;

I want to separate type declaration (and possibly it's genercics) between few macroses to match on different parameters. I see it like this:

macro_rules! get_type {
    (false) => {
        RectangleA
    }
    (true) => {
        RectangleB
    }
}

macro_rules! impl_something {
    ($SOME_FLAG: expr) => {
        get_type!($SOME_FLAG)::<5, 10>
    }
}

7 posts - 4 participants

Read full topic

🏷️ rust_feed