Self not recognized in blocks macro parameters
⚓ Rust 📅 2025-07-18 👤 surdeus 👁️ 17Hi!
I'm currently trying to create a macro that involves having as a parameter the block of a function,
as in this example:
macro_rules! implement {
(
$name:ty,
fn $func:ident(&self) $block:block
) => {
impl $name {
fn $func(&self) $block
}
}
}
struct Foo {
bar: i32,
}
implement! {
Foo,
fn print_bar(&self) {
println!("bar: {}", self.bar);
}
}
even though cargo expand's shows the correct expansion (which includes &self in the function signature), this code doesn't compile.. The compiler gives this error:
expected value, found module `self` [E0424]
`self` value is a keyword only available in methods with a `self` parameter
So, I wanted to ask:
- Is this behavior expected, or am I doing something wrong?
- Are there any plans to make this work in the future? (e.g. with the new
macrosyntax) - What would be a valid workaround here?
Thanks!
2 posts - 2 participants
🏷️ rust_feed