Related Articles
Scalar Datatypes in Rust
• Last Updated : 03 Mar, 2021

All initialized variables use data-type during declaration to restrict the type of data to be stored. Therefore, we can say that data types are used to tell the variables the type of data they can store.

In this article, we will specifically talk about the Scalar Data types in the Rust programming language. A Scalar type in Rust represents a single value. For example, an int data type only stores single integer data and not multiple integers nor a combination of different data types.

Rust has four primary scalar types:

• Integers
• Floating-point numbers
• Boolean
• Characters

Let’s discuss them in detail.

### Integer types

Integers are everywhere, Integer is a whole number with a sign and without a fractional part. For example 1, 5, -9, 200001, -15489752are integers

Integers are one of the primitive data types in Rust, Rust compiler can automatically detect Integers and also the programmer can explicitly define integer type.

Example:

## Rust

 `fn` `main() {``   ` `      ``// implicitly defining int``    ``let` `x = 6;``   ` `      ``// explicity defining unsigned int``    ``let` `y: u16 = 9;``   ` `      ``// explicitly defining signed int``    ``let` `z: i16 = -7;``    ``println!(``"{} {} {}"``, x,y,z);``}`

Output:

`6 9 -7`

In Rust, integer declaration indicates that the value it’s associated with should be an unsigned integer that starts with u (or) signed integer types start with i and takes up 32 bits of space with a minimum of 8 bits.

The signed complement integers types have:

### Floating-point types

Floating-point numbers integers with the decimal parts. For example 3.22, -5.89, 200.0000. Rust also has a primitive datatype for floating numbers. Rust’s floating-point types are either f32 and f64, which are 32 bits and 64 bits in size, respectively. Like Integers, you can both explicitly and implicitly define floating-point numbers. Syntax of floating-point numbers

## Rust

 `fn` `main() {``// implicit floating number type``let` `x = 200.000;``// explicitly floating point type``let` `y: f64 = 9.894;``let` `z: f64 = -7.4;``println!(``"{} {} {}"``, x,y,z);``}`

Output

`200 9.894 -7.4`

### Boolean types

Boolean type is one of the built-in data types provided by Rust, which are defined by the True or False keywords. Generally, it is used to represent the truth values of the expressions. Boolean types in Rust are provided with 1-bit memory. The Boolean type in Rust is specified using the bool keyword.

## Rust

 `fn` `main() {``// true boolean``let` `x: ``bool` `= ``true``;``// false boolean``let` `y: ``bool` `= ``false``;``println!(``"{} {}"``, x,y);``}`

Output:

`true false`

### Character types

The character data type is used for storing characters. The keyword used for the character data type is char. Characters typically require 1 byte of memory space and range from -128 to 127 or 0 to 255.

Example:

## Rust

 `fn` `main() {``    ``let` `s = ``'z'``;``    ``let` `c = ``'ℤ'``;``      ``println!(``"{} {}"``, s,c);``}`

Output

`z ℤ`

Simple Rust program with all scalar datatypes:

## Rust

 `fn` `main() {``   ` `        ``// Default is "i32"``        ``let` `g = 9;``        ``println!(``"{}"``, g);``   ` `        ``// Default is "f64"``        ``let` `f = 2.9;``        ``println!(``"{}"``, f);``   ` `        ``// Add explicit type``        ``let` `e: i64 = 14122020;``        ``println!(``"{}"``, e);``   ` `        ``// Boolean``        ``let` `is_active: ``bool` `= ``true``;``        ``println!(``"{}"``, is_active);``   ` `        ``// char``        ``let` `c = ``'a'``;``        ``println!(``"{}"``, c);``        ``let` `face = ``'\u{1F600}'``;``        ``println!(``"{}"``, face);``}`

Output:

```9
2.9
14122020
true
a
????```
My Personal Notes arrow_drop_up