Ich möchte ein Makro schreiben, das" OK "ausgibt und dann self in einer Methode zurückgibt. Es ist mein erstes Makro, so habe ich versucht, diese, dachte, es wird nur so etwas wie ein Text Ersatz machen, aber es funktioniert nicht:Kann nicht self in Makro verwenden, da die Makroerweiterung das Token `self` ignoriert
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!()
}
}
Fehler:
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!()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
Nach einem kurzen Blick auf die Dokumentation, I Ich weiß, dass es nicht nur ein Ersatz für Text ist, aber ich weiß immer noch nicht, wie es funktioniert.