Damian
Damian

Reputation: 4631

can isize and usize be different in rust?

Can isize and usize be different? Both of them can be used for memory size, index, offset.

Since usize is used for arrays why don't we just have usize

I am new to Rust so this might be a basic question.

Update: On a 32 bit system they are both 32 bit long and on a 64 bit system they are both 64 bit long. Irrespective of the sign.

Upvotes: 9

Views: 9388

Answers (2)

Peter Hall
Peter Hall

Reputation: 58715

On a 32 bit system, isize is the same as i32 and usize is the same as u32. On a 64 bit system, isize is the same as i64 and usize is the same as u64.

  • usize cannot be negative and is generally used for memory addresses, positions, indices, lengths (or sizes!).
  • isize can be negative, and is generally used for offsets to addresses, positions, indices, or lengths.

Upvotes: 33

yellowB
yellowB

Reputation: 3020

isize is architecture-based(e.g. 32bit/64bit) signed(negative/0/positive) integer type.

See here:

Primitive Type isize

The pointer-sized signed integer type.

See also the std::isize module.

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.

usize is architecture-based(e.g. 32bit/64bit) unsigned(0/positive) integer type.

See here:

Primitive Type usize

The pointer-sized unsigned integer type.

See also the std::usize module.

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.

Upvotes: 5

Related Questions