"Expected one of `(` or `<`, found `$`" when generating function code with a macro in Rust

41 Views Asked by At

To avoid writing hundreds of lines of repetitive code - I am trying to use declarative macros (macro_rules!) to generate the struct method code. For some reason though, I am getting a confusing error Expected one of '(' or '<', found '$', where it should have expanded a metavariable, $x.

This a snippet of my code:

//...

macro_rules! LD_X_Y {
        ($x:ident, $y:ident) => {
            pub fn ld_$x_$y(&mut self, opcode: u8) {
                self.cpu.set_$x(self.cpu.get_$y());
            }
        };
}

impl GB {
    //...

    LD_X_Y!(a, b);

    //...
}

The error is indicated at the $x in the function name ld_$x_$y (line 3). I have been using this question as well as The Rust Reference section on macros as reference.

Does anyone know why this doesn't work? Thank you.

0

There are 0 best solutions below