Rust, Software Development

Rust Core Data Types To Get Familiar With

Rust has two types of core data types – scalar and compound. Scalar types are single-value types, while compound types are multi-value types.

Integer Data Types

An integer data type is one of Rust’s core data types that represents a numeric value without a fractional component. For instance, 1 is a value of integer type; whereas, 1.99 is not. There are several integer data types in Rust, and each data type represents a different range of values.

Integer TypeSigned / UnsignedLength (bits)Range
i8Signed8-128 to 127
u8Unsigned80 to 255
i16Signed16-32,768 to 32,767
u16Unsigned160 to 65,535
i32Signed32-2,147,483,648 to 2,147,483,647
u32Unsigned320 to 4294967295
i64Signed64 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
u64Unsigned640 to 18,446,744,073,709,551,615
i128Signed128-1.7014118346046923e+38 to 7014118346046923e+38
u128Unsigned1280 to 3.4028236692+e38
isizeSigned32 on 32-bit architecture

or

64 on 64-bit architecture
-2,147,483,648 to 2,147,483,647

or

-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
usizeUnsigned32 on 32-bit architecture

or

64 on 64-bit architecture
0 to 4294967295

or

0 to 18,446,744,073,709,551,615

Rust Floating-Point Data Types

A floating-point type represents a numeric value with a fractional component. For instance, 1.99 is a value of floating-point type, whereas 200 is not.

Floating-point TypeSigned/UnsignedLength (bits)Range
f32Signed32approximately ±3.40282347E+38F
(6-7 significant decimal digits)
Rust implements IEEE 754 standard
f64Signed64approximately ±1.79769313486231570E+308
(15 significant decimal digits)

When we use a floating-point value in our codes, it defaults to f64. To verify this, run the following codes.

These codes generate this output:

Boolean Data Type

A boolean type a 16-byte type that represents the true or false value.

Boolean TypeSigned/UnsignedLength (bits)Range
boolNot Applicable8true or false only

Rust Character Data Type

The Character data type (or char) typically represents a single letter value enclosed with single quotes. For example:

The char data type is also a single 16-bit Unicode character. It has a minimum value of ‘\u{0000}’ (or 0) and a maximum value of ‘\u{ffff}’ (or 65,535 inclusive). For example:

It can also refer to a non-Alphabetical character. For example:

Array Type

Unlike the previous Rust’s core data types, an Array type represents a collection of more than one value (or element) of the same data type. Hence, it is a compound type.

Declaring and initializing arrays is straightforward.

There are two ways to access each value in the array – using [] and the get() function.

However, there is only one way to modify each element value – using [].

So far, the collections of values we’ve briefly touched on are all single-dimensional. Arrays can also be multi-dimensional.

Tuple Type

A Tuple type is akin to the Array type except that it can have values of different types. For example, we can have a tuple with an integer, an character, and an array of f64.

String Type

A String type is a collection of elements of character type enclosed with double-quotes.

There are some things we need to know about Strings when learning Rust. Understanding these, among other things, will make learning the advanced stuff in Rust easier to grasp.

This post is now part of Rust Programming Language For Beginners Tutorial.

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like