Permalink

0

Rust Primitive Types

Rust offers a lot of the regular cast of types, many with some interesting twists.

The character type is called char, but unlike the common 1-byte ASCII characters in C, Rust characters are 4-byte Unicode. We can define character variables enclosing character values in single quotes:

Numeric types are named by bit size and signedness, with signed integer types beginning with “i” and unsigned types beginning with “u”, while floating-point types are always signed:

Rust also offers the types isize and usize, the bit-capacity of which is platform-dependent, but their “range is sufficient to express the size of any collection”, quoth the Rust documentation. [I presume that we’ll find out later what sorts of collections this refers to; I imagine that one of these types may be the type of the size of arrays or lists or some-such, though I’m not sure why we would need two different types, much less a signed version.]

Arrays are collections of values of the same type:

The type of the array consists of two components, the type of the elements within the array (char in the above example), and the size of the array (5 in the example).

If we want to have a collection of a values of a mixture of types, Rust provides tuples:

Here the type of the tuple indicates the type of each value in the tuple, in order. As usual in Rust, we could let the compiler infer the types if we don’t need, say, some particular size of number.

More: read the Primitives Types chapter in The Rust Programming Language.

Leave a Reply

Required fields are marked *.