Is there a macro or similar workaround to include the source folder (src) path at compile time?

Viewed 1329

Is there a Rust macro or a similar workaround to include the path of the 'src' folder created via cargo new in my source file as a string literal at compile time or specifically when doing cargo build?

I have successfully done something similar where I use include_str! to include file content but I need to know if it is possible to directly include the src path in the code.

1 Answers

No, but you can get close using 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());
}

Outputs, on the playground:

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