{ println!("Macro called !") -6ren">
gpt4 book ai didi

rust - 将静态字符串传递给rust中的macro_rule

转载 作者:行者123 更新时间:2023-12-03 11:41:24 28 4
gpt4 key购买 nike

我写了以下代码:

macro_rules! my_macro{
("A") => {
println!("Macro called !")
}
}

fn main(){
static test: &'static str = "A";
my_macro!(test);
}

但我有以下错误:
error: no rules expected the token `test`
--> test.rt:9:19
|
1 | macro_rules! my_macro{
| --------------------- when calling this macro
...
9 | my_macro!(test);
| ^^^^ no rules expected this token in macro call

error: aborting due to previous error

但是,如果我直接调用 my_macro("A"),它会很好地工作。有可能解决这个问题吗?

最佳答案

Is it possible to fix this ?



否。在解析项目名称之前,宏会在编译时扩展,因此您的宏不知道 test的值是什么(即使是 const而不是 static也不知道)。

关于rust - 将静态字符串传递给rust中的macro_rule,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61448375/

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