Is it possible to access current file name?

Viewed 169

Is it possible to access current file name in Rust by

// main.rs
fn main() {
    println!("filename: {}", FILE_NAME);
}

?

(This program should print filename: main.rs)

1 Answers

You can use the std::file macro to get the current source filename at the compile time.

let this_file = file!();

If you want to remove the path from the returned filename, you can construct a Path with it and call the file_name method.

let filename_only = Path::new(this_file).file_name().and_then(|s| s.to_str()).unwrap();

Playground

Related