问题描述
在 C++ 中,您可以使用诸如 __clang_version__
之类的东西.Rust 有类似的东西吗?我在互联网上搜索,但一无所获.
In C++, you could use something like __clang_version__
. Is there something similar for Rust? I searched on the internet, but found nothing.
推荐答案
不直接.
rustc_version
crate 告诉你 rustc
在命令行上访问;这旨在用于构建脚本.还有 rustc_version_runtime
做了类似的事情,但将信息公开为运行时调用(即 它在编译时检测编译器版本,但在运行时暴露它.
There is the rustc_version
crate which tells you the version of rustc
accessible on the command-line; this is designed to be used in a build script. There's also rustc_version_runtime
which does something similar, but exposes the information as a runtime call (i.e. it detects the compiler version at compile time, but exposes it at runtime).
标准免责声明:非常小心编写任何依赖于编译器版本的内容.理想情况下,您应该只测试支持使用 semver 的功能的最低版本(上述两个库都直接支持).
Standard disclaimer: be very careful writing anything that depends on compiler version. You should ideally only test for minimum versions for which features are supported using semver (which both of the above libraries support directly).
这篇关于有没有办法从 Rust 程序中检测编译器版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!