gpt4 book ai didi

rust - 从 macro_rules 返回值!在使用rust

转载 作者:行者123 更新时间:2023-11-29 08:08:24 25 4
gpt4 key购买 nike

<分区>

我正在浏览 Rust by Examples - Macros/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)
}
}

现在我希望我的自定义宏 calculate 返回计算值。我尝试了以下内容:

macro_rules! calculate {
(eval $e:expr) => {{
let val: usize = $e;
println!("{} = {}", stringify!{$e}, val);
val
}};
}

但它返回错误说 error[E0308]: mismatched types in val, expected type (), found type i32

如何修改上述宏以返回计算值?谢谢。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com