简体   繁体   English

如何在Rust中跨平台获取可执行扩展?

[英]How to get the executable extension across platforms in Rust?

I'm writing portable Rust code to look for a program in some directories. 我正在编写可移植的Rust代码来查找某些目录中的程序。 On Windows I expect it will be foo.exe and elsewhere just foo . 在Windows上我希望它将是foo.exe和其他地方只是foo

Obviously I could just use if cfg!(windows) but that seems ugly. 显然我可以使用if cfg!(windows)但这看起来很难看。

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 . 是的, std::env::consts::EXE_SUFFIX在Windows上是.exe ,而std::env::consts::EXE_EXTENSION将是exe Both are empty on Unix. 两者在Unix上都是空的。

These can be combined with the base name using for example std::path::PathBuf::set_extension . 这些可以使用例如std::path::PathBuf::set_extension与基本名称组合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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