简体   繁体   English

在Rust宏中操纵路径?

[英]Manipulate paths in Rust macro?

I want to write a macro that can break apart a path to manipulate its components. 我想编写一个宏,它可以分解一条路径来操纵其组件。 For example: 例如:

macro_rules! example {
    ($path:path) => {
        vec![
            stringify!(get_path_init!($path)),
            stringify!(get_path_last!($path)),
        ]
    };
}

fn main() {
    let path_parts = example!(std::vec::Vec);
    assert_eq!(path_parts, vec!["std::vec", "Vec"]);
}

Does anything exist like this in the standard library or any reasonably popular crates, and is it possible to implement with macros? 在标准库或任何相当流行的板条箱中是否存在这样的东西,并且可以使用宏来实现吗? Or would it require a compiler plugin? 还是需要编译器插件?

It would require a compiler plugin; 这将需要一个编译器插件。 this cannot be done with macro_rules! 使用macro_rules!无法做到这macro_rules! , nor with anything that's part of the language or the standard library, and any crates that did do it (not that I know of any) would require a nightly compiler anyway . ,也没有与任何的语言或标准库,并没有做任何的板条箱(不,我知道任何)的一部分, 无论如何都会需要每晚编译器。

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

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