Nathan Ringo
Nathan Ringo

Reputation: 1003

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?

Upvotes: 2

Views: 911

Answers (1)

DK.
DK.

Reputation: 59155

It would require a compiler plugin; this cannot be done with 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.

Upvotes: 5

Related Questions