Built-in data types in Rust Programming Language

Rust

In this post, we are going to learn Built-in data types in Rust Programming Language with code examples. Rust programming languages support built-in Data types. The Data types are the basic blocks that control the validity of the variable values and store them accordingly.

Like all modern languages, the Rust compiler is smart and can automatically infer the type of a variable when a certain type of value is assigned to it. So you can say it does compile-time type deduction automatically.

Program declaring variables

So let’s see the different Data types Supported by Rust: In this example, as you can see we are declaring the variable with type “let” and assigning them values of different types supported by Rust. So compiler itself will deduce the type by the types of values assigned to the variables.

fn main() 
{
let myName = “Sachin”;       // This is type string
let Careeravg = 57.68           // This is type float
let isRetired = true               // This is Boolean type
}

This means data type is optional while declaring a variable in Rust. You can declare variables like this:

// Here no type is specified 
let variable_A = value; 
//Here type is specified as datatype                  
let variable_B:dataType = value;  

// Here no type is specified so by value it will be integer
let variable_A = 4;  
 //Here type is specified as bool
let variable_B:bool = true;

Rust Programing Built in Data types


Like most of the languages, Rust has built-in support for the following Data types.

  • Integer
  • Boolean
  • Characters
  • Floating-Point

Integer

The integer type is used to store the whole number values. To further classify integer in rust supports signed integer type to store both negative and positive values, and unsigned integer type to support only positive values.

The range of the integer depends on the system you are using for example it will vary depending on 32 bit machine or 64 bit machine.

Float

The float type is used to store the decimal number values.

Boolean

Boolean type variables can be assigned true or false values.  The declaration and assignment can be done like this:

fn main() {let isRetired:bool = true;}

Character

Rust supports character datatype where you can store numbers, alphabets, Unicode characters and also special characters.

The declaration and assignment of character variables can be done like this:

fn main() {let myNameInitial:char = ‘S’;let mySymbol = ‘ $‘;}c