是否存在Rust宏或类似的解决方法,以便在编译时或在执行cargo new
时将源文件中通过cargo build
创建的'src'文件夹的路径包含为字符串文字?
我已成功完成类似的操作,我使用include_str!
来包含文件内容,但我需要知道是否可以在代码中直接包含src路径。
答案 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());
}
FILE: "/playground/src/main.rs"
src path: Some("/playground/src")