`usize` 和 `u32` 有什么区别? [英] What's the difference between `usize` and `u32`?
问题描述
文档说 usize
是
指针大小的无符号整数的操作和常量.
Operations and constants for pointer-sized unsigned integers.
在大多数情况下,我可以将 usize
替换为 u32
并且没有任何反应.所以我不明白为什么我们需要两种如此相似的类型.
In most cases, I can replace usize
with u32
and nothing happens. So I don't understand why we need two types which are so alike.
推荐答案
正如文档所述,usize
是指针大小的,因此其实际大小取决于您编译程序的架构.
As the documentation states usize
is pointer-sized, thus its actual size depends on the architecture your are compiling your program for.
例如,在 32 位 x86 计算机上,usize = u32
,而在 x86_64 计算机上,usize = u64
.
As an example, on a 32 bit x86 computer, usize = u32
, while on x86_64 computers, usize = u64
.
usize
保证始终足够大以容纳数据结构中的任何指针或任何偏移量,而 u32
在某些架构上可能太小.
usize
gives you the guarantee to be always big enough to hold any pointer or any offset in a data structure, while u32
can be too small on some architectures.
这篇关于`usize` 和 `u32` 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!