windows - 为什么 usize::max_value() 在 64 位 Windows 上返回无符号 32 位整数的最大值?

标签 windows rust

documentation备注:

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

但是当我运行一个简单的测试时:

use std::usize;

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

它打印:4294967295

我的项目刚刚初始化并且有两行 use std::usize;println!("{}", usize::max_value()); 已添加,其他未更改。

我的 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

删除 Rust 并使用 64 位 Windows rustup 安装程序重新安装后,我得到:

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

但是当我运行 rustup toolchain list 时,它会打印出一个项目:

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

这里发生了什么?

最佳答案

正如 usize 的文档所说:

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 是 32 位整数的最大大小;因此表明您正在为 32 位平台编译。

您的 rustc 输出证实了这一点:

host: i686-pc-windows-msvc

您已经安装了 32 位 Windows 编译器。 64 位编译器说 x86_64-pc-windows-*

您可以将默认的 rustup 主机更改为 64 位:

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

然后您可能需要卸载并重新安装稳定的工具链以将其切换到 64 位。

另见:

关于windows - 为什么 usize::max_value() 在 64 位 Windows 上返回无符号 32 位整数的最大值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60527989/

相关文章:

encryption - Rust 中的 HMAC、RS 和 Base64

windows - 删除 node_modules 文件夹

c# - Windows 上的视频叠加(使用 C#?)

rust - 无法定义适当的防 rust 生命周期要求

rust - 如何实现一个链表的加法?

rust - Rust 集合是否有能力拥有它们存储的数据?

c# - 当应用程序进程被终止/崩溃时终止子进程

c++ - 顶点和索引缓冲区如何在 DirectX11 中与顶点、法线和 Texcoords 一起工作

windows - 将自定义 "new folder"添加到 Windows 资源管理器上下文菜单

rust - 递归异步函数中的函数参数