The confusion for newcomers with Rust Developement given they have worked in other languages

In rust we have to use i32, i64 for defining the data type instead of like int in Java/C/C++ or auto-detecting in python is there any way to understand this easily or is it something considered to be a problem only for newcomers. Will it be easy to handle with experience?

let a: i32 = 100;

The different integer types aren't very unusual once you get used to them. Is there a particular point of confusion we could help clarify?

2 Likes

Java, C, and C++ all have integer types of different sizes just like Rust. They just have different names.

8 Likes

C also has the uintXX_t types that are exactly the same as rust's uXX types.

1 Like

Rust also has usize and isize if you don’t need to be explicit about the width.

2 Likes

You mean "like short and int and long and ... in Java/C/C++".

Also types like char that are very different between those languages.

Does this section of the Rust book on integers help?

So how do you know which type of integer to use? If you’re unsure, Rust’s defaults are generally good places to start: integer types default to i32. The primary situation in which you’d use isize or usize is when indexing some sort of collection.

I'm curious, what. programming languages don't have a selection of different number types for different sizes of signed integers, unsigned integers and floating point numbers?

The only ones I can think of are old Javascript, before it got BigInts and typed buffers. Even old Javascript made a distinction between 64 bit floats and 32 bit integers, in its own way. Or old style BASIC back in the 1980's. Oh, and the simple language I wrote a toy compiler for once that only had 32 bit integers.

1 Like

it is also possible that OP means explicit type annotations in general, but used integers as example from some introductory book snippets...
If this is the case, to answer such question - specific types have to be there in the end everytime, but many times compiler can "guess"/identify these from context (if possible) and you dont need to write them yourself into code...

1 Like

At first I thought the same, but in Java/C/C++, you do have to use type annotations. Unless they mean the very specific corner case where you have to specify the type of a variable because it gets used later on in an ambiguous fashion.

Python is different from the others - the integers you see there are "BigInts" - they are nice because they never overflow. The price for that of course is they don't have a fixed size.

If you are looking for something like that in rust, take a look at the bigint crate.