What is "usize"?

Hello!

What kind of a type is usize? I see it is used a lot for ranges, but I do not understand what it stands for. Unsigned size I guess, but what does that mean? Why not just use some appropriate u-int type like u8 or u64? Is it trying to convey the idea that the int is limitless? If so, how do you actually implement “limitless” for an int?

Best regards,
Catalin

3 Likes

Hi Catalin,

usize is the the natural unsized integer for the platform. (Can’t think of the right word). That means on 64 bit computers its a u64 and in 32bit it’s a u32.

4 Likes

usize is the ponter size on the given architecture. u64 for 64-bit CPUs, u32 for 32-bit processors.

9 Likes

To follow up on why this is important/useful, Pointing out that it’s the pointer size (sorry, not sorry, for the pun), is helpful. Usize allows you to point to whatever memory you can hold on the machine in question. Having that as a specific type allows portability between the two architectures.

2 Likes

Currently it is documented as “unsigned pointer sized integer”.
See: Proposal: `usize` definition should be refined · Issue #5185 · ziglang/zig · GitHub

No, usize is limited. std.math.big provides types for limitless integers and rational numbers (it is actually limited by the amount of memory).

4 Likes