Instagram
youtube
Facebook
Twitter

Data Types

  • In rust every variable has a data type which tells rust to know which kind data type is being used to specific variable and value and how to work with that data. So, Rust must know the type of data at the compilation because rust is a statically typed language.
  • Primitive data types are:

    Numbers
    Boolean
    Characters
    String
    Array
    Slice
    Tuple

About Primitive data types:

These are the basic building block of any programming language and these are already defined by the programming language to provide built-in support.

Rust signed Integers: i8, i16, i32, i64 bits

Rust unsigned Integers: u8, u16, u32, u64

Rust float: these are used to contain decimal components. Rust provides f32 and f64 types used for single and double-precision numbers.

Boolean: The truth values, which might be True or False, are represented using the Boolean data type. The Boolean operators are frequently employed in decision-making statements.

Here are the sample code of these data types

fn main() {

    let mut x = 45; // i32

    let y: i64 = 81; //i64

    // floating point numbers 
    let _f: f32 = 6.7;
    

    let _b: bool = false;

    println!("y: {}", y);
    println!("Hello, world!");
    println!("The value of x is {}", x);

    x = 60;

    println!("The value of x is {}", x);

    let a = true;
    let b: bool = false;
    println!("The value of a is: {}", a);
    println!("The value of b is: {}", b);

    let chr1 = 'K';
    let chr2 = 'Y';
    println!("{}", chr1);
    println!("{}", chr2);

    let emp = ["Steve", "John", "Alex"]; 
    println!("The second employee is: {}", emp[1]);
}
  • Custom data types are:

    Structure

    we can use arrays after we wish to hold multiple parts of the uniform knowledge sort in a single variable, however, what if we wish to carry heterogeneous knowledge sort part, victimization structure you'll be able to wrap one or additional variables that will be in several knowledge sorts into one.

    Enumeration

    An enumeration may be a set of predefined named values, known as members. Enumerations square measure helpful after we wish to contend with a restricted set of values for variables.