"no rules expected the token" 在宏中编写 `let` 语句时
"no rules expected the token" when writing a `let` statement in a macro
当我尝试使用 rustc main.rs
:
编译此代码时
macro_rules! getPI {
let pi = 3.141592;
println!("Pi is roughly 3.142 \n {0}", pi);
}
fn main() {
print!(getPI);
}
它给我一个错误:
error: no rules expected the token `pi`
--> src/main.rs:2:9
|
2 | let pi = 3.141592;
| ^^
我对编程很陌生,希望有人能提供解决方案。
如果你是"very new to programing",那么你应该从头开始;宏不是的开始。返回并重新阅读 The Rust Programming Language, second edition,即使它针对的是已经了解另一种编程语言的人。
然后您可以阅读 the chapter from the first edition about macros。这将向您展示宏的正确语法。宏有很多分支,比如 match
语句:
macro_rules! getPI {
() => {
let pi = 3.141592;
println!("Pi is roughly 3.142 \n {0}", pi);
}
}
fn main() {
getPI!();
}
我也不知道您为什么要尝试 print!
宏的 return 值,所以我删除了它。您还必须使用感叹号 (!
).
调用宏
当我尝试使用 rustc main.rs
:
macro_rules! getPI {
let pi = 3.141592;
println!("Pi is roughly 3.142 \n {0}", pi);
}
fn main() {
print!(getPI);
}
它给我一个错误:
error: no rules expected the token `pi`
--> src/main.rs:2:9
|
2 | let pi = 3.141592;
| ^^
我对编程很陌生,希望有人能提供解决方案。
如果你是"very new to programing",那么你应该从头开始;宏不是的开始。返回并重新阅读 The Rust Programming Language, second edition,即使它针对的是已经了解另一种编程语言的人。
然后您可以阅读 the chapter from the first edition about macros。这将向您展示宏的正确语法。宏有很多分支,比如 match
语句:
macro_rules! getPI {
() => {
let pi = 3.141592;
println!("Pi is roughly 3.142 \n {0}", pi);
}
}
fn main() {
getPI!();
}
我也不知道您为什么要尝试 print!
宏的 return 值,所以我删除了它。您还必须使用感叹号 (!
).