Search code examples
windowspathrustfilenames

How to get the executable extension across platforms in Rust?


I'm writing portable Rust code to look for a program in some directories. On Windows I expect it will be foo.exe and elsewhere just foo.

Obviously I could just use if cfg!(windows) but that seems ugly.

Is there a better way to find the platform's executable file extension?


Solution

  • Yes, std::env::consts::EXE_SUFFIX will be .exe on Windows, and std::env::consts::EXE_EXTENSION will be exe. Both are empty on Unix.

    These can be combined with the base name using for example std::path::PathBuf::set_extension.