是否有一个宏或类似的解决方法在编译时包含源文件夹(src)路径?

时间:2017-11-15 08:09:45

标签: compilation rust code-generation rust-cargo rust-macros

是否存在Rust宏或类似的解决方法,以便在编译时或在执行cargo new时将源文件中通过cargo build创建的'src'文件夹的路径包含为字符串文字?

我已成功完成类似的操作,我使用include_str!来包含文件内容,但我需要知道是否可以在代码中直接包含src路径。

1 个答案:

答案 0 :(得分:3)

不,但您可以使用file!

来结束
const FILE: &'static str = concat!(env!("CARGO_MANIFEST_DIR"), "/", file!());

fn main() {
    use std::path::Path;

    println!("FILE: {:?}", FILE);
    println!("src path: {:?}", Path::new(FILE).parent());
}

输出,on the playground

FILE: "/playground/src/main.rs"
src path: Some("/playground/src")