I want to write a macro that prints "OK" then returns self in a method. It's my first macro, so I tried this, thinking it will just make something like a text replacement, but it fails:
macro_rules! print_ok_and_return_self { () => { println!("OK"); self } } fn main() { let a = A{}; a.a().a(); } struct A {} impl A { fn a(self) -> Self { print_ok_and_return_self!() } } Error:
error: macro expansion ignores token `self` and any following --> src/main.rs:4:13 | 4 | self | ^^^^ | note: caused by the macro expansion here; the usage of `print_ok_and_return_self!` is likely invalid in expression context --> src/main.rs:17:13 | 17| print_ok_and_return_self!() | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ After a quick look at the documentation, I know it's not just text replacement, but I still don't know how to make it work.