The idea behind usize

I am wondering what is the idea behind having a separate usize datatype?

Why not simply use something like i32/i64 or their unsigned counterparts? Is there an advantage to having usize?
I don't mean to start a philosophical discussion, but there must be a reason it was created.

https://doc.rust-lang.org/book/ch03-02-data-types.html

Additionally, the isize and usize types depend on the architecture of the computer your program is running on, which is denoted in the table as “arch”: 64 bits if you’re on a 64-bit architecture and 32 bits if you’re on a 32-bit architecture.

they abstract from the architecture on which the program is running(some sort of generics)

2 Likes

Using i32 for indexes is rather limiting on 64-bit platforms, and would prevent you from using arrays longer than 4 GB. Using i64 for indexes would be rather wasteful on 32-bit platforms.

7 Likes

It's a decades old idea.

8 Likes

It's defined as the size of a pointer, and that varies per platform.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.