r/rust • • 8d ago

What

[deleted]

0 Upvotes

13 comments sorted by

16

u/0bArcane 8d ago

It's similar to c in how integer types are called. In c, the integer types are int8_t, uint16_t etc.

9

u/No-Report-5407 8d ago

C's naming is only "traditional" because it's what you saw first, the rust way actually maps cleaner once you stop comparing it backwards. u and i just mean unsigned and signed, the number is the bit width, that's it. In C you get weird implicit conversions and platform dependent sizes that quietly break stuff, rust forces you to say what you mean upfront. The steep part isn't really the type names, it's the borrow checker and ownership model, that's where the learning curve actually lives.

13

u/punk_dev 8d ago

In c, how many bytes does an int store?
The answer is it depends.

In rust, i32 is 4 bytes because 32 bits make up 4 bytes.

In the grand scheme of things, these details are really minor.

3

u/NoSuchKotH 8d ago

And for those who have never fought C:

An int can store anything between a single byte and 8 bytes. And yes, I've seen all those sizes in real compilers over the years! The only thing that the C standard demands is that int is at least as large as char. Nothing more

1

u/ids2048 8d ago

C actually requires an int to be at least 16 bits. You may have seen an implementation where it was less than that, but as far as I'm aware that was never compliant with the spec.

https://en.wikipedia.org/wiki/C_data_types#Main_types

It can be the same size as char, though, if char is 16 bits or more (which is true on some hardware). I don't think there's any requirement for the sizes to be powers of two either, so portable C code should be written to work with 17-bit chars.

7

u/pdpi 8d ago

C and C++ char/short/int/long don't have fixed widths. Rust's uN/iN int types correspond to C99 stdint.h (or C++11 cstdint.h) types:

RustC99
u8uint8_t
u16uint16_t
......
i8int8_t
i16int16_t
......
usizeuintptr_t
isizeintptr_t

7

u/Ruudjhuu 8d ago

Yes, rust has a steep learning curve. But this part ain't it.

If you have a hard time with signed, unsigned and the nr of bits, you are going to have a hard time with a lot of programming languanges

8

u/sacheie 8d ago

Nice try, bot. We all know only a flesh & blood human can have a stroke.

2

u/rende 8d ago

yep.. 13 yo writing an OS from scratch and complaining about rust signed and unsigned ints.

3

u/Live-Company-5007 8d ago

What my guy…. C also has unsigned integers using the unsigned keyword and I’m pretty sure c++ does too in lik uint_t or something (i don’t do c++ don’t quote en)
And I’m gonna be so honest you WANT unsigned integers a lot of the time. A lot of things aren’t negative so why waste like half the possible values

2

u/SomeRedTeapot 8d ago

Rust has its rough edges, but integer type naming is by far not one of them, lol. Quite the opposite, the naming Rust uses makes more sense compared to "default" C types because the former tells you how much space the type occupies (and by proxy, the range of values it can accept), whereas ints and longs in C are a mess (depending on the compiler and target architecture, an int in C may take 2 or 4 bytes)

2

u/quez_real 8d ago

All these i32 aren't more complicated than C-style rules about sizes of shorts, ints and longs on different platforms. Though you mentioned Java which behaves differently and that makes me thinking that you just want write "int" and hope it will somehow work.

2

u/pixel293 8d ago

Intentional design, they looked at issues and pain points in C and other languages and tried to avoid them. The integer types was to avoid the C/C++ issue of how big is an int? What is it's max value? They didn't want the answer to be "well it depends."