# Whether to use type isize or i32, fsize or f64, usize or u32

**URL:** <https://users.rust-lang.org/t/whether-to-use-type-isize-or-i32-fsize-or-f64-usize-or-u32/86303>\
**Category:** help\
**Created:** [December 21, 2022, 6:09pm UTC](https://users.rust-lang.org/t/whether-to-use-type-isize-or-i32-fsize-or-f64-usize-or-u32/86303 "2022-12-21T18:09:29Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![preacherdad](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/preacherdad/32/30932_2.png) [@preacherdad](https://users.rust-lang.org/u/preacherdad)\
**Post date:** [December 21, 2022, 6:09pm UTC](https://users.rust-lang.org/t/whether-to-use-type-isize-or-i32-fsize-or-f64-usize-or-u32/86303/1 "2022-12-21T18:09:29Z")

</div>

This is a request for clarification. As I've been working through the Rust book and other learning resources, sometimes I see, for instance, `isize` used and sometimes I see `i32`, or something else more specific. I like the idea of using `isize` so I've been using it quite a bit, but sometimes it throws an error when I don't expect it. In my mind I'm wondering why we even bother with `i32` or `f64` or `u32` when the more generic `isize`, `fsize`, and `usize` are available. Can someone clarify how all this fits together? Thanks.

---

<div class="post-metadata">

**Author:** ![riley-martin](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/riley-martin/32/30701_2.png) [@riley-martin](https://users.rust-lang.org/u/riley-martin)\
**Post date:** [December 21, 2022, 6:17pm UTC](https://users.rust-lang.org/t/whether-to-use-type-isize-or-i32-fsize-or-f64-usize-or-u32/86303/2 "2022-12-21T18:17:07Z")

</div>

`i32`, `f64`, etc. are a fixed size, but the `isize` and `usize` can be different sizes depending on the target machine and OS

---

<div class="post-metadata">

**Author:** ![VorfeedCanal](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/vorfeedcanal/32/22432_2.png) [@VorfeedCanal](https://users.rust-lang.org/u/VorfeedCanal)\
**Post date:** [December 21, 2022, 6:17pm UTC](https://users.rust-lang.org/t/whether-to-use-type-isize-or-i32-fsize-or-f64-usize-or-u32/86303/3 "2022-12-21T18:17:14Z")

</div>

> [@preacherdad](#):
>
> In my mind I'm wondering why we even bother with `i32` or `f64` or `u32` when the more generic `isize`, `fsize`, and `usize` are available.

Because program which doesn't ever talk to the outside world is not very interesting, ultimately.

> [@preacherdad](#):
>
> Can someone clarify how all this fits together?

`fsize` doesn't exist. You use `isize` or `usize` when you don't deal with externally defined APIs or file formats.

Most of the time `f64` is good, but something you need to do a lot of calculations and then `f64` is just slow. Same with integer sizes.

But 90% of time (maybe even 99%) you used You use `i8`/`u8`/etc simply because some externally-defined APIs use these types.

---

<div class="post-metadata">

**Author:** ![preacherdad](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/preacherdad/32/30932_2.png) [@preacherdad](https://users.rust-lang.org/u/preacherdad)\
**Post date:** [December 21, 2022, 6:24pm UTC](https://users.rust-lang.org/t/whether-to-use-type-isize-or-i32-fsize-or-f64-usize-or-u32/86303/4 "2022-12-21T18:24:40Z")

</div>

Ok, so let's see if I've got this straight. `isize` and `usize` are fine unless my program is talking to the outside world, so my choice of, say, `i32` would be because the data I'm pulling in is of `i32` type. Is that what you're saying?

---

<div class="post-metadata">

**Author:** ![VorfeedCanal](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/vorfeedcanal/32/22432_2.png) [@VorfeedCanal](https://users.rust-lang.org/u/VorfeedCanal)\
**Post date:** [December 21, 2022, 6:31pm UTC](https://users.rust-lang.org/t/whether-to-use-type-isize-or-i32-fsize-or-f64-usize-or-u32/86303/5 "2022-12-21T18:31:03Z")

</div>

90% of time. You may also need it to make your program a bit faster, but that's much less often.

Most of the time something externally-imposed determines your choice.

E.g. [Unicode](https://en.wikipedia.org/wiki/Unicode) defines `char` as something between `0` and `1048575`.

That's too much to fit into `i16` or `u16` thus `char` uses 4 bytes (means you can convert it to `u32` or, usually, into `usize`, but not into `i16` or `u16`).

---

<div class="post-metadata">

**Author:** ![user16251](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/user16251/32/30527_2.png) [@user16251](https://users.rust-lang.org/u/user16251)\
**Post date:** [December 21, 2022, 6:38pm UTC](https://users.rust-lang.org/t/whether-to-use-type-isize-or-i32-fsize-or-f64-usize-or-u32/86303/6 "2022-12-21T18:38:36Z")

</div>

It depends on what you're doing and the hardware you're using.

- You have to use `usize` to index arrays.
- If you know overflow cannot occur, 32-bit integers are usually preferable to 64-bit integers. They generate slightly smaller code on x86 and take up half as much memory.
- It's easier to reason about signed arithmetic. Unsigned arithmetic has some weird pitfalls.
- Unsigned types are usually better when you need to work with the underlying bits (e.g. if you're working with a bitstream, `[u64]` is probably your best bet).
- Floating point types (`f64` and `f32`) are for representing real numbers, typically not integers, but you can exactly represent integers up to 53 bits in an `f64`. If you are writing algorithms with floating point numbers you have to make a choice about speed vs accuracy.

---

<div class="post-metadata">

**Author:** ![preacherdad](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/preacherdad/32/30932_2.png) [@preacherdad](https://users.rust-lang.org/u/preacherdad)\
**Post date:** [December 21, 2022, 6:45pm UTC](https://users.rust-lang.org/t/whether-to-use-type-isize-or-i32-fsize-or-f64-usize-or-u32/86303/7 "2022-12-21T18:45:50Z")

</div>

So, am I right that using `isize` and `usize` as much as possible will make my code more portable to other OS's? (Using Linux right now.) I figure that the less specific my code is (as in `i32` being more specific than `isize`), the easier it will be to port to, say Windows. Is that a good way to think about it?

---

<div class="post-metadata">

**Author:** ![ZiCog](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/zicog/32/12216_2.png) [@ZiCog](https://users.rust-lang.org/u/ZiCog)\
**Post date:** [December 21, 2022, 7:06pm UTC](https://users.rust-lang.org/t/whether-to-use-type-isize-or-i32-fsize-or-f64-usize-or-u32/86303/8 "2022-12-21T19:06:09Z")

</div>

Well, if you have a huge amount of data then you might want to use the smaller sizes to save on memory and or disk storage space, assuming your data number ranges fit the type. So i8, u8, i16, u16 can be your friends.

Even if memory spaces is not an issue I suspect more cache friendly code can be produced if more data fits in the caches. So smaller types might be appropriate for performance critical code.

Given that isize and usize can change their width depending on platform it seems to me that for maximum predictability when changing platforms on should use specific sizes rather than usize.

My tendency is to use the largest size possible unless there is a specific reason to use something smaller, as above. I'm keen on using unsigned types for things I really don't want to be negative.

---

<div class="post-metadata">

**Author:** ![VorfeedCanal](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/vorfeedcanal/32/22432_2.png) [@VorfeedCanal](https://users.rust-lang.org/u/VorfeedCanal)\
**Post date:** [December 21, 2022, 7:16pm UTC](https://users.rust-lang.org/t/whether-to-use-type-isize-or-i32-fsize-or-f64-usize-or-u32/86303/9 "2022-12-21T19:16:44Z")

</div>

> [@preacherdad](#):
>
> Is that a good way to think about it?

I would say it's good way to think about it **for a novice**.

The reasons to use `i8` or `u16` **do** exist (and you may read about them in answers here) but 90% of time `isize`/`usize` are “good enough”.

So go with `isize`/`usize` for now and return to that question after year or two.

All these things about caches, memory consumption and many other things **may** be important, even such exotics things as `f16` (present [in modern CPUs](https://en.wikipedia.org/wiki/F16C) but not yet in Rust) can be useful.

But… later. Learn to write robust, stable code first… then you may start to think about squeezing efficiency from it.

---

<div class="post-metadata">

**Author:** ![preacherdad](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/preacherdad/32/30932_2.png) [@preacherdad](https://users.rust-lang.org/u/preacherdad)\
**Post date:** [December 21, 2022, 7:24pm UTC](https://users.rust-lang.org/t/whether-to-use-type-isize-or-i32-fsize-or-f64-usize-or-u32/86303/10 "2022-12-21T19:24:15Z")

</div>

Thanks, guys. That has really helped me get a better idea of how to use number types in Rust. Appreciate your responses.

---

<div class="post-metadata">

**Author:** ![ZiCog](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/zicog/32/12216_2.png) [@ZiCog](https://users.rust-lang.org/u/ZiCog)\
**Post date:** [December 21, 2022, 7:32pm UTC](https://users.rust-lang.org/t/whether-to-use-type-isize-or-i32-fsize-or-f64-usize-or-u32/86303/11 "2022-12-21T19:32:51Z")

</div>

> [@preacherdad](#):
>
> So, am I right that using `isize` and `usize` as much as possible will make my code more portable to other OS's? (Using Linux right now.)

I have to say no, this is not right.

The Rust Reference sates:

> `usize` and `isize` have a size big enough to contain every address on the target platform. For example, on a 32 bit target, this is 4 bytes and on a 64 bit target, this is 8 bytes.

Potentially it could be 16 bits or not even a multiple of 8 bits. Rust compiles for the Arduino which has a very small memory space, I have no idea what an isize/usize is there.

For me then, to avoid surprises I would not use isize/usize. Heck, you cannot even count the human population using usize on some platforms/operating systems.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/scottmcm/32/4286_2.png) [@scottmcm](https://users.rust-lang.org/u/scottmcm)\
**Post date:** [December 21, 2022, 7:34pm UTC](https://users.rust-lang.org/t/whether-to-use-type-isize-or-i32-fsize-or-f64-usize-or-u32/86303/12 "2022-12-21T19:34:06Z")

</div>

> [@preacherdad](#):
>
> when the more generic `isize`, `fsize`, and `usize` are available

The cost of `usize` is that you don't know where the overflow behaviour is. So you should only use it for things where its domain is exactly right: things representing memory. Notably, that means slice indexes and fenceposts.

If you need values that aren't natural numbers, then you probably want floating-point numbers, and pick between `f32` and `f64` depending on how much relative error you can tolerate.

Otherwise, use the unsigned number that's the best fit for what you're trying to express, considering the set of values you need to express. `u32` is a reasonable default -- though if you're _storing_ tens of thousands of something then it's worth considering a smaller type, as wider local variables is often free.

(Personally, I think that signed integers are mostly useless in computing -- that unsigned integers or floating-point numbers are superior in the majority of cases.)

---

<div class="post-metadata">

**Author:** ![ZiCog](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/zicog/32/12216_2.png) [@ZiCog](https://users.rust-lang.org/u/ZiCog)\
**Post date:** [December 21, 2022, 7:52pm UTC](https://users.rust-lang.org/t/whether-to-use-type-isize-or-i32-fsize-or-f64-usize-or-u32/86303/13 "2022-12-21T19:52:14Z")

</div>

> [@scottmcm](#):
>
> I think that signed integers are mostly useless in computing --

How so? Many things I deal with everyday can be negative and mostly don't need to be floats.

I'm old school, still thinking of floats as a luxury, processors often don't have float hardware.

One old boss of mine said to a new member of our team who was using floats in his code "If you think you need to use floating point arithmetic to solve the problem then you don't understand the problem". Mind you our target had no float support and our language had good fixed point arithmetic support.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/scottmcm/32/4286_2.png) [@scottmcm](https://users.rust-lang.org/u/scottmcm)\
**Post date:** [December 21, 2022, 8:08pm UTC](https://users.rust-lang.org/t/whether-to-use-type-isize-or-i32-fsize-or-f64-usize-or-u32/86303/14 "2022-12-21T20:08:30Z")

</div>

> [@ZiCog](#):
>
> Mind you our target had no float support and our language had good fixed point arithmetic support.

Sure, fixed-point is fine too. Though those are translation-invariant instead of scale-invariant, and it's not at all obvious to me that there are more translation-invariant problems than scale-invariant ones. Certainly most things we deal with IRL care more about relative error than absolute errors. There are certainly [some](https://www.moddb.com/members/kevryan/blogs/the-butterfly-effect-deterministic-physics-in-the-incredible-machine-and-contraption-maker) things where fixed-point is better, though, since scaling doesn't really exist.

---

<div class="post-metadata">

**Author:** ![ZiCog](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/zicog/32/12216_2.png) [@ZiCog](https://users.rust-lang.org/u/ZiCog)\
**Post date:** [December 21, 2022, 8:27pm UTC](https://users.rust-lang.org/t/whether-to-use-type-isize-or-i32-fsize-or-f64-usize-or-u32/86303/15 "2022-12-21T20:27:17Z")

</div>

There is a corollary to my old bosses statement about floats:

> If you really do need floating point to solve the problem then you will have problems you won't understand.

Basically: [What Every Computer Scientist Should Know About Floating-Point Arithmetic](https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html)

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/cad97/32/5076_2.png) [@CAD97](https://users.rust-lang.org/u/CAD97)\
**Post date:** [December 21, 2022, 8:33pm UTC](https://users.rust-lang.org/t/whether-to-use-type-isize-or-i32-fsize-or-f64-usize-or-u32/86303/16 "2022-12-21T20:33:12Z")

</div>

I'd like to second scottmcm and zicog here: if it's an arbitrary choice and all other things are equal, you should use a fixed size integer type, not `isize`/`usize`.

All Rust code is "portable" in the way C code is "portable" — it's capable of being ported. However, `isize`/`usize` aren't _target independent_, so the behavior of code is less consistent across targets/platforms if it uses them instead of the fixed size types, which behave the exact same on all targets.

If you don't constrain the type of an integer literal, it's an `i32`. (For floating point, `f64`.) There's a logical reasoning behind this choice — all else being equivalent, these are fairly reasonable defaults to choose.

The first guideline is to use the same type the APIs you're feeding into use. Mostly, that means that if you're doing indexing, use `usize`, but you might also be using a library that deals in e.g. `u16`, and if you're interacting with that API, you should probably store and handle those as `u16` on your side as well.

If that's not the case and you're using floating point, just default to `f64`. You still have to deal with floating point inaccuracies, but they're less than with `f32`. If you want `f32` instead, you probably already know that or are using a library using `f32` (e.g. computer graphics).

If you want integers, 32 bit is a fine default. 2 billion is still a lot, and the important thing is that you'll get the same behavior on whatever targets, rather than e.g. if you're using `isize` perhaps seeing overflow only on one older system that happens to be 32-bit but none of the newer or development machines see this occuring.

If overflow happens in debug mode, you'll get a panic (crash) pointing out exactly where the overflow occurred, so you can determine if the overflow is an error (fix the error or handle the overflow) or just an expected case (in which case you can refactor the type to use 64-bit integers). In release mode, you'll get silent wrapping overflow by default (but you can change back to panicking overflow by configuration).

Math on `i32` is essentially\[1\] never going to be slower than math on `isize` and might instead be faster, since the CPU doesn't have to process as much data. This is the most apparent to the optimizer rather than the physical CPU, notably when the optimizer is able to vectorize your code to use SIMD and process multiple data chuncks in a single iteration. The smaller your data, the more single-core parallelism you can get (e.g. `i32x8` versus `f64x4`).

* * *

I agree that generally you'll typically want `u32` instead of `i32`, because most integer measures are counts or offsets or such with a contextually known sign, rather than one value which could logically be either positive or negative. (This doesn't change that `i32` is a better default if no context is given other than "it's an integer.") The obvious large category which excepts that is physical measure, but that generally is a rational number rather than an integer, and in today's compute landscape that means `f64` except in specialty cases where fixed point (integer of partial unit) is preferred instead. Currency is a fun example: you'd perhaps first assume that should be `i64` cents, but in commerce the sign is known from context (prices are positive, discounts are negative respectively, final order total should probably only ever be positive) and a bank account being allowed to overdraft into debt is a special case. Using signed numbers for "real world" things is always convenient and perhaps even the correct default, but you should always consider what a negative value means; if a negative value has unclear or no meaning, prevent it with an unsigned value.

> [@scottmcm](#):
>
> translation-invariant problems than scale-invariant ones. Certainly most things we deal with IRL care more about relative error than absolute errors.

If measures vary wildly in scale, then scale-invariance is indeed typically more important. Where scale is relatively constant, though, scale-invariance obviously doesn't matter much, an translation-invariance is extremely beneficial. [I love Contraption Maker as an example here](https://www.gamedeveloper.com/design/the-butterfly-effect). More things with semifixed known scale should imho be using translation-invariant rather than scale-invariant real numbers, but the hardware support for IEEE floats is hard to pass up, and scale-invariance absolutely ends up offering progressive degradation opposed to fixed-scale needing to handle overflow and such. Scale independence is essentially built-in significant figures, and for the purpose of measurement rather than creation/simulation, that's hugely beneficial. (I'm just steeped in the latter.)

* * *

1. Some exceptions: 16-bit targets with 16 bit `isize`, and perhaps some _extremely_ minor (maximum order of one cycle per branch, closer to one cycle per function call) hits on some 64-bit targets where ABI passes 32 bit integers zero extended in 64 bit registers and does not provide any 32 bit operations, thus requiring masking 32 bit integers before function calls.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/scottmcm/32/4286_2.png) [@scottmcm](https://users.rust-lang.org/u/scottmcm)\
**Post date:** [December 21, 2022, 8:43pm UTC](https://users.rust-lang.org/t/whether-to-use-type-isize-or-i32-fsize-or-f64-usize-or-u32/86303/17 "2022-12-21T20:43:06Z")

</div>

> [@ZiCog](#):
>
> > If you really do need floating point to solve the problem then you will have problems you won't understand.

Or you'll learn how to use the tool so you _do_ understand the problems. Just like borrowing in Rust -- one often has problems that initially they don't understand, but as they learn more they know how to handle the complex cases and avoid the troublesome ones.

For example,

> To start, let me answer your question directly. For JPL's highest accuracy calculations, which are for interplanetary navigation, we use 3.141592653589793. Let's look at this a little more closely to understand why we don't use more decimal places. I think we can even see that there are no physically realistic calculations scientists ever perform for which it is necessary to include nearly as many [360] decimal points as you asked about.
> 
> ~ [How Many Decimals of Pi Do We Really Need? - Edu News | NASA/JPL Edu](https://www.jpl.nasa.gov/edu/news/2016/3/16/how-many-decimals-of-pi-do-we-really-need/)

Why would they state that number specifically, as opposed to more or fewer digits? I've got a pretty good guess:

[`[src/main.rs:2] std::f64::consts::PI = 3.141592653589793`](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=cc13fc87b185592f4247b760fa2de5d0)

---

<div class="post-metadata">

**Author:** ![ZiCog](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/zicog/32/12216_2.png) [@ZiCog](https://users.rust-lang.org/u/ZiCog)\
**Post date:** [December 21, 2022, 8:50pm UTC](https://users.rust-lang.org/t/whether-to-use-type-isize-or-i32-fsize-or-f64-usize-or-u32/86303/18 "2022-12-21T20:50:23Z")

</div>

> [@scottmcm](#):
>
> Or you'll learn how to use the tool so you _do_ understand the problems.

I agree. That takes time and experience. As I said my boss's pronouncement was made to a very junior, "green horn" member of the team. Kind of makes more sense in that context.

> [@scottmcm](#):
>
> Just like borrowing in Rust -- one often has problems that initially they don't understand, but as they learn more they know how to handle the complex cases and avoid the troublesome ones.

Disagree. You see if a neophyte does not understand how to keep the borrow checker happy he cannot produce code that even compiles. Rust saves him from himself. Contrast to the weird things that can happen with naive use of floats, without any compiler errors or warnings.

I would agree, if you had said "Just like borrowing in languages like C and C++". Where years of experience with the debugger teaches one about lifetimes.

---

<div class="post-metadata">

**Author:** ![ZiCog](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/zicog/32/12216_2.png) [@ZiCog](https://users.rust-lang.org/u/ZiCog)\
**Post date:** [December 21, 2022, 9:04pm UTC](https://users.rust-lang.org/t/whether-to-use-type-isize-or-i32-fsize-or-f64-usize-or-u32/86303/19 "2022-12-21T21:04:29Z")

</div>

> [@CAD97](#):
>
> typically want `u32` instead of `i32`, because most integer measures are counts or offsets or such with a contextually known sign,

I don't buy this. Plenty of sensors I deal with produce negative outputs. Using signed integers saves having to keep track of sign by other means. Also how can I test a value for being between a lower and a higher threshold without signed arithmetic.

> [@CAD97](#):
>
> The obvious large category which excepts that is physical measure, but that generally is a rational number rather than an integer,

But, but, I have never used a sensor that did not present an integer output. It may well be some odd scale like 16th of a degree C. Sticking such things in floats is already claiming more accuracy than is possible.

I think the reality is most programmers, most of the time, including me, are lazy and just uses floats because it saves us worrying about overflows and fractions most of the time. We only worry about it when we get surprised by odd results.

2 billion, or 4 billion is a ridiculously small number 🙂

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/scottmcm/32/4286_2.png) [@scottmcm](https://users.rust-lang.org/u/scottmcm)\
**Post date:** [December 21, 2022, 9:14pm UTC](https://users.rust-lang.org/t/whether-to-use-type-isize-or-i32-fsize-or-f64-usize-or-u32/86303/20 "2022-12-21T21:14:00Z")

</div>

> [@ZiCog](#):
>
> Contrast to the weird things that can happen with naive use of floats, without any compiler errors or warnings.

I'm unsure what you're proposing instead, though. You mentioned fixed-point earlier, but it has many of the same problems.

A 16.16 fixed-point has 1 / 3 \* 3 = 0.99998 != 1, for example. It happens to get lucky with the classic float example of 0.1 + 0.2 = 0.3, but that's not because it solved the problem: it has 0.1 + 0.3 = 0.40001 != 0.4.

[Next page](https://users.rust-lang.org/t/whether-to-use-type-isize-or-i32-fsize-or-f64-usize-or-u32/86303.md?page=2)
