2

A boolean determining if it's 64-bit is perfect but an integer representing the amount of bits would also be fine.

I want to capture some information about the PC's architecture for statistics purposes.

tversteeg
  • 4,717
  • 10
  • 42
  • 77
  • 2
    Basically a duplicate of [how to find if the machine is 32bit or 64bit](https://stackoverflow.com/questions/2401756/how-to-find-if-the-machine-is-32bit-or-64bit). – Shepmaster Jun 20 '17 at 14:21
  • 1
    @Shepmaster that question is about [tag:c] while this one is about [tag:rust]. – tversteeg Jun 20 '17 at 14:23
  • 2
    So? The question has nothing to do with Rust. By definition, it's asking about how to query the OS for information, which neither Rust the language nor Rust's standard library deal with. – Shepmaster Jun 20 '17 at 14:24
  • Also a duplicate of [https://stackoverflow.com/q/2140619/155423](https://stackoverflow.com/questions/2140619/correct-way-to-check-if-windows-is-64-bit-or-not-on-runtime-c) – Shepmaster Jun 20 '17 at 14:25
  • I don't think this question is a duplicate of the ones you linked since they don't provide any information which would help me answer this question. – tversteeg Jun 20 '17 at 14:35
  • 1
    You probably also need to define *which* operating systems you need to care about. macOS, Linux, Windows, one of the BSDs, Redox, etc. Rust also allows targeting systems that don't have operating systems. – Shepmaster Jun 20 '17 at 14:35
  • Well I was looking for a general solution but since that's not possible: Mac OS, Linux & Windows. Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/147176/discussion-between-tversteeg-and-shepmaster). – tversteeg Jun 20 '17 at 14:41
  • Possible duplicate of [How to check in Rust if architecture is 32 or 64 bit?](https://stackoverflow.com/questions/41896462/how-to-check-in-rust-if-architecture-is-32-or-64-bit) – Daniel Fath Jun 20 '17 at 17:08
  • @Daniel that question is about compile time, I want to know it runtime – tversteeg Jun 20 '17 at 18:23

2 Answers2

2

In the best case your program is already compiled for the correct architecture/target. This means that you already know at compile time whether or not the program is being compiled for a 32bit or 64bit target. You can check that by using the cfg() attribute or the cfg!() macro:

fn is_compiled_for_64_bit() -> bool {
    cfg!(target_pointer_width = "64")
}

#[cfg(target_pointer_width = "32")]
fn foo() {
    println!("foo compiled for 32 bit");
}

#[cfg(target_pointer_width = "64")]
fn foo() {
    println!("foo compiled for 64 bit");
}

But in case you want to ship only 32-bit binaries to your users, your program is then executed either natively by the user's 32 bit hardware or in compatibility mode by the user's 64 bit hardware. To find our whether your program actually runs on a 32 bit architecture or just in a 32 bit compatibility mode is more difficult and depends on your operating system. I'm not aware of any easy cross platform way to do that. I would advise you to compile separately for each architecture you're targeting anyway.

Lukas Kalbertodt
  • 79,749
  • 26
  • 255
  • 305
  • 1
    The question is asking if the OS is 64 or 32 bit. Not the application. – mason Jun 20 '17 at 14:22
  • 2
    @mason that part is addressed by the paragraph with "To find our whether your program actually runs on a 32 bit architecture or just in a 32 bit compatibility mode ..." – Shepmaster Jun 20 '17 at 14:40
-1

You can use this:

#[cfg(target_os="your operating system here")]
// code here

This literally just checks if the operating system the program was compiled on is the recommended one. Then I guess you can do this to check the bits of the operating system:

#[cfg(all(unix, target_pointer_width = "32"))]
// code here

Where unix is the operating system (just a placeholder, should support Windows etc.), and 32 is the OS bitness.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
lolman
  • 1
  • 2
  • [Lukas Kalbertodt's answer](https://stackoverflow.com/a/44655636/155423) already says what this answer does; perhaps you could be clear about what this new answer brings? This also has the same limitations - it won't report 64 bits when run in a compatibility environment. – Shepmaster Jun 23 '17 at 17:14
  • @Shepmaster, where to find list of valid values for "target_os" and similar parameters? Thanks. – Bulat M. Jan 17 '18 at 16:19
  • @BulatM. a [basic Google search](https://www.google.com/search?q=site:stackoverflow.com%20rust%20%20valid%20values%20for%20%22target_os%22) leads to [Is there a list of all cfg features?](https://stackoverflow.com/q/41742046/155423). – Shepmaster Jan 17 '18 at 16:53