如果体系结构是 32 位还是 64 位,如何检查 Rust?

How to check in Rust if architecture is 32 or 64 bit?

是否有条件检查处理器是 32 位还是 64 位?我正在寻找一种配置检查,例如#cfg[x86]cfg[x64].

cfg section in the Rust reference 中的 #[cfg(target_pointer_width = "64")] 似乎是一个可能的解决方案。它是基于指针的大小(以及isizeusize),这应该与架构相对应。

您应该查看 conditional compilation 上的 Rust 参考章节:

target_arch = "..." - Target CPU architecture, such as "x86", "x86_64", "mips", "powerpc", "powerpc64", "arm", or "aarch64". This value is closely related to the first element of the platform target triple, though it is not identical.