我正在通过示例了解Rust - 宏/DSL
代码显示:
macro_rules! calculate {
(eval $e:expr) => {{
{
let val: usize = $e; // Force types to be integers
println!("{} = {}", stringify!{$e}, val);
}
}};
}
fn main() {
calculate! {
eval 1 + 2 // hehehe `eval` is _not_ a Rust keyword!
}
calculate! {
eval (1 + 2) * (3 / 4)
}
}
Run Code Online (Sandbox Code Playgroud)
现在我希望我的自定义宏calculate返回计算值。我尝试了以下内容:
macro_rules! calculate {
(eval $e:expr) => {{
let val: usize = $e;
println!("{} = {}", stringify!{$e}, val);
val
}};
}
Run Code Online (Sandbox Code Playgroud)
但它返回我错误说error[E0308]: mismatched types in val, expected type (), found type i32。
如何修改上述宏以返回计算值?谢谢。
| 归档时间: |
|
| 查看次数: |
2783 次 |
| 最近记录: |