- Stuff that's brand new, and I have no existing mental model for
Given that reality, I figured a post was in order! So without further ado, let's talk data types.
Additionally, The Rust Reference mentions primitive types, which includes
Never on top of the four scalar types.
Let's start with something familiar, booleans. Booleans, or
bool in Rust, are a type with two possible values:
The same can't be said of numbers. In Rust, you need to care about what type of number you're storing. Is it an integer or a floating point number? What amount of space, in bits, does storing it take? Is it signed or unsigned?
In Rust you can initialize integers of five different sizes, from 8-bit to 128-bit, either signed or unsigned. For floating point numbers you have either f32 or f64; the first is single precision and the second, double precision.
You may have noticed that I didn't mention strings as Rust scalar types, I mentioned
char. This is accurate. However, Rust does have a primitive type
str. The strange thing is you likely won't use it other than as a reference to a specific string stored in memory,
If you want to create or manipulate strings, you want the growable type
String. But be careful! The methods you're used to, like
len (aka length), won't return what you expect.
array is a valid Rust type. Unfortunately, that's not entirely helpful. An array in Rust is of fixed size. If you expect to create static data that won't change but you want to reference/look it up in an array structure then this may be what you want.
More likely, though, you want a Vector.
vec is similar to
String. It's an array type that you can own and grow dynamically.
Search "Rust object type" and you're bound to be confused. You'll end up reading up on Trait object types, which is a bit more advanced than what you're after. What you're really looking for is
Note that there are also BTree based implementations of these structures.
If you can't find what you're looking for the docs are great! And so is the community. With a little bit of trial and error you'll get what you need.