简体   繁体   中英

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM