Why does usize::max_value() return the maximum value of an unsigned 32 bit integer on 64 bit Windows?

Viewed 996

The documentation notes:

assert_eq!(usize::max_value(), 18446744073709551615);

But when I run a simple test:

use std::usize;

fn main() {
    println!("{}", usize::max_value());
}

It prints: 4294967295

My project has only been initialised and had the 2 lines use std::usize; and println!("{}", usize::max_value()); added, nothing else has been changed.

My output of rustc --version --verbose:

rustc 1.41.1 (f3e1a954d 2020-02-24)
binary: rustc
commit-hash: f3e1a954d2ead4e2fc197c7da7d71e6c61bad196
commit-date: 2020-02-24
host: i686-pc-windows-msvc
release: 1.41.1
LLVM version: 9.0

After deleting Rust and reinstalling using the 64-bit Windows rustup installer, I get:

Current installation options:
  default host triple: x86_64-pc-windows-msvc
  default toolchain: stable
  profile: default
  modify PATH variable: yes

But when I run rustup toolchain list it prints a single item:

stable-i686-pc-windows-msvc (default)

What is happening here?

1 Answers

As the documentation for usize says:

The size of this primitive is how many bytes it takes to reference any location in memory. For example, on a 32 bit target, this is 4 bytes and on a 64 bit target, this is 8 bytes.

4294967295 is the maximum size of a 32-bit integer; thus indicating that you are compiling for a 32-bit platform.

This is corroborated by your rustc output:

host: i686-pc-windows-msvc

You have installed the 32-bit Windows compiler. The 64-bit compiler says x86_64-pc-windows-*.

You can change the default rustup host to 64-bit:

rustup set default-host x86_64-pc-windows-msvc

You will then probably need to uninstall and reinstall the stable toolchain to switch it to 64-bit.

See also:

Related