In this tutorial, Learn about Data types in Rust. Every variable declared in Rust holds values of data type.

There are two types of data types.

Rust Scalar Types:

It represents a single value.

  • Integers,: positive or negative numbers
  • Floating Point Numbers,: Numbers with precision
  • Characters: Single character or unicode or emoji
  • Booleans: represents true or false

For example, 12,12.3, ‘a’, true are scalar types

Compound Types:

It is used to combine multiple types into a single type.

Integer Data type in Rust

Integer types store numbers without fractional digits. It is also called whole numbers.

Two types of integer types exist. Signed and unsigned.

Signed Integer:

Stores the positive and negative numbers. Examples are -20 and 35. It stores the different bytes and isize.

isize is a size of an integer, It is dependent on the OS machine.

32 bits machine such as X86 has support for 32-bits 64 bits machine such as X64 has support for 64-bits

Following is a representation of different positive and negative numbers

Integer TypeSupported Maximum valuesBits
i8-27 to 27-18 Bits
i16-215 to 215-116 Bits

|i32| -231 to 231-1| 32Bits| |i64| -263 to 263-1| 64 Bits| |i128| -2127 to 2127-1| 128 Bits| |isize| -231 to 231-1 or -263 to 263-1| 32 or 64 Bits|

unsigned Integer:

Stores the only positive numbers. Examples are 45,21.

usize is a size of an unsigned integer, It is dependent on the OS machine.

32 bits machine such as X86 has support for 32-bits 64 bits machine such as X64 has support for 64-bits

Following is a representation of different positive numbers

Integer TypeSupported Maximum valuesBits
u80 to 28-18 Bits
u160 to 216-116 Bits
u320 to 232-132Bits
u640 to 264-164 Bits
u1280 to 2128-1128 Bits
usize0 to 28-1 or 0 to 264-132 or 64 Bits

Let’s see an example The below is a valid assignment

fn main() {
    let number: i32 = -120;
    let number1: u32 = 120;
    let number2 = 120;

    println!("{}", number);
    println!("{}", number1);
    println!("{}", number2);
}

This example below assigns the negative value to an unsigned integer.

fn main() {
    let number: i32 = -120;
    let number1: u32 = -120; // throws an error
    let number2 = 120;

    println!("{}", number);
    println!("{}", number1);
    println!("{}", number2);
}

Output:

It throws unsigned values cannot be negated and cannot apply unary operator-to typeu32``

error[E0600]: cannot apply unary operator `-` to type `u32`
 --> test.rs:3:24
  |
3 |     let number1: u32 = -120;
  |                        ^^^^ cannot apply unary operator `-`
  |
  = note: unsigned values cannot be negated.

Floating Numbers in Rust

Floating numbers contain numbers with decimal numbers.

It supports two types of float types f32 and f64.

f32: float32 is 32 bits in size and single precision supported IEE floating values f64: float64 is 64 bits in size and double-precision that supports IEE floating values

if you don’t declare a type with floating values, the default is f32.

Here is an example of understanding

fn main() {
    let float1: f32 = 120.33121322312312312;
    let float2: f64 = 120.33121322312312312;
    let float3 = 120.56000;

    println!("{}", float1);
    println!("{}", float2);
    println!("{}", float3);
}

Output:

120.331215
120.33121322312313
120.56

Another example, when you assigned an integer to a floating type

fn main() {
    let float1: f32 = 120;

    println!("{}", float1);
}

It throws the compilation error mismatched types.

The reason is Rust compile does not do automatic conversion of one type to another type

Rust boolean datatype

bool is one of the scalar types that have only true and false values.

Boolean is of one byte in size and used in if-else, while conditional expression.

fn main() {
    let boolean1 = false;
    let boolean2: bool = true;
    let boolean3 = 12 < 3;
    let boolean4 = true && false;

    println!("{}", boolean1);
    println!("{}", boolean2);
    println!("{}", boolean3);
    println!("{}", boolean4);
}

Output:

false
true
false
false

Rust Character datatype

Rust provides char to represent a single character. It stores letters, alphabets, special characters, and Unicode characters.

Characters are declared with characters enclosed in single quotes.

Example characters ‘E’, ‘1’,’_’.

Each char in Rust represents the Unicode value that starts with U. The Example Unicode value is U+0000.

fn main() {
    let character1 = 'e';
    let character2: char = '_';
    let character3 = 'R';
    let character4 = '😃';

    println!("{}", character1);
    println!("{}", character2);
    println!("{}", character3);
    println!("{}", character4);
}

Output:

e
_
R
😃