poolie
poolie

Reputation: 9526

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?

Upvotes: 1

Views: 480

Answers (1)

poolie
poolie

Reputation: 9526

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.

Upvotes: 5

Related Questions