Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

rust

processor

If there a conditional check for whether processor is 32-bit or 64-bit? I'm looking for kind of configuration check like e.g. #cfg[x86] or cfg[x64].

like image 431
Daniel Fath Avatar asked Jan 27 '17 14:01

Daniel Fath


People also ask

Is Rust 32 bit or 64 bit?

It's both. Once you install rustup , you can switch between 32 and 64 bit with rustup defaut … and platform from this list, such as i686-pc-windows-msvc or x86_64-pc-windows-msvc (or equivalent for Linux, Mac, etc.

Is x86 architecture 32 bit?

x86 is the name of the architecture that it's built to run on (the name comes from a series of old Intel processors, the names of which all ended in 86, The first of which was the 8086). Although x86 was originally a 16-bit architecture, the version in use today is the 32-bit extension.


2 Answers

The #[cfg(target_pointer_width = "64")] from the cfg section in the Rust reference seems like a likely solution. It is based on the size of a pointer (as well as isize and usize), which should correspond to the architecture.

like image 134
Daniel Fath Avatar answered Sep 28 '22 07:09

Daniel Fath


You should check the Rust Reference chapter on conditional compilation:

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.

like image 36
ljedrz Avatar answered Sep 28 '22 05:09

ljedrz