rust - 将宏匹配器插入字符串文字中

标签 rust

此片段打印 $i ,但我希望它打印 foo 。我已经尝试了这个主题的一些变体,但没有成功,我也无法在文档中找到有关此行为的任何内容。有语法可以实现这一点吗?

macro_rules! print_ident {
    ($i:ident) => {
        println!("$i");  
    };
}

fn main() {
    print_ident!(foo);
}

最佳答案

Yes .

macro_rules! print_ident {
    ($i:ident) => {
        println!(stringify!($i));  
    };
}

fn main() {
    print_ident!(foo);
}

关于rust - 将宏匹配器插入字符串文字中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47217869/

相关文章:

rust - 进行多次可变和不可变借贷的最佳实践是什么? [复制]

stream - 使用 PhantomData 和 unsafe 将流式迭代器作为普通迭代器处理

iterator - 如何向 Iterator 添加新方法?

rust - simplelog::CombinedLogger 在运行时 panic

rust - 在 Rust 中环绕负数

closures - 在多个闭包之一中捕获相同的变量

unit-testing - 如何处理测试单元中的中止

c++ - 将Rust项目链接到与其他cmake项目链接的cmake项目

rust - 一次不能多次借用 `rdr` 作为可变的 [当使用 cargo 的库时]

rust - 如何从actix SyncContext向另一个参与者发送消息?