简体   繁体   English

Rust 宏规则:? 如何写一生?

[英]Rust macro_rules!: How to write lifetimes?

I'm trying to create a lifetime-generic function within a macro.我正在尝试在宏中创建一个终身通用的 function 。 Nothing fancy from the outside, just a hardcoded function-local one:从外面看没有什么花哨的,只是一个硬编码的本地函数:

macro_rules! generate_parse_function {
    ($rule_name:ident, $parse_expr:tt) => {
        fn $rule_name<'a>(code: &'a str) -> (Option<Ast>, &'a str) {
            (Ast::$rule_name, $parse_expr)
        }
    };
}

But no matter what I do it comes out like this:但无论我做什么,结果都是这样的:

fn nil< 'a>(code: & 'astr) -> (Option<Ast>, & 'astr){
    (Ast::nil,"NIL")
}

There's a space before 'a (which may or may not be a problem), and the space is removed between a and str (which is definitely a problem). 'a之前有一个空格(这可能是也可能不是问题),并且在astr之间删除了空格(这绝对是一个问题)。 I can't find anything about this on the internet, but it must be a reasonably common scenario.我在互联网上找不到任何关于此的信息,但它一定是一个相当常见的场景。 Anybody know what to do here?有人知道在这里做什么吗?

Bizarrely, this appears to be a bug in rust-analyzer .奇怪的是,这似乎是rust-analyzer中的一个错误。 It reports the wrong result but when I run cargo-expand manually, I get the correct result (as pointed out by @eggyal).它报告了错误的结果,但是当我手动运行cargo-expand时,我得到了正确的结果(正如@eggyal 所指出的那样)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM