[英]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.