How to use `paste!` to concatenate double colons?
⚓ Rust 📅 2025-10-06 👤 surdeus 👁️ 5Hi, I am trying to create a macro_rules to help generate a function, which reduce many duplicated code. The function is just simple setters/getters.
Here's the example:
It is actually not the real code in my project, but I try to create a minimal example that can reproduce my question.
And one more thing, let's ignore the details about
struct OptionBuilderin below example, it doesn't matter.
#[macro_export]
macro_rules! builder_impl {
($member:ident,$flags:ident,$($field:ident),+) => {
paste::paste! {
impl OptionBuilder {
$(
pub fn $field(&mut self, value: bool) -> &mut Self {
let mut flags = self.$member.unwrap_or( [< $member:upper >] );
flags.set( [< $flags :: $field:upper >] , value);
self.$member = Some(flags);
self
}
)+
}
}
}
}
I will use this macro like this:
struct Flags {
const EXPAND_TAB = 1;
}
const FLAGS: Flags = Flags {};
builder_impl!(flags, Flags, expand_tab);
And it should generate source code:
impl OptionBuilder {
pub fn expand_tab(&mut self, value: bool) -> &mut Self {
let mut flags = self.flags.unwrap_or( FLAGS );
flags.set( Flags::EXPAND_TAB , value); <-- Compiler complains about the double colons in "Flags::EXPAND_TAB"
self.flags = Some(flags);
self
}
}
The error comes from the double colons in the Flags::EXPAND_TAB, it seems paste! macro thinks the :: is other things.
How should I fix this?
3 posts - 2 participants
🏷️ Rust_feed