Rust - Data Types . ( In 6 Points !)

Rust - Data Types . ( In 6 Points !)

integers, floats, Boolean, char ,tuple and array .

This is a part of my journey while learning rust (for web dev), so a said why not share it ,Let's go a head and see the data types ; feel free to see the my full rust journey and give it a star : go there

Scalar data type (integers, floats, Boolean, char) immutable

integers :

  • u (no negative)
  • i (positive & negative) //types : 8,16,32,64,128

    EX:

let num:u32=16;
let num:i16=-136;

Note: 8:0-255, 16:0-65,535, 32:-2m-2m .


floats :

  • f32
  • f64(default) (only)

    EX:

    let num:f32=16.23;
    

Boolean:

  • bool

    EX:

    let is_num:bool=true;
    

    Note: you can use 0|1 instead .


char :

  • char

    EX:

    let letter:char='a';
    

    Note: any type as long as it's single_quotation || one_char .


Compound data type (tuple , array) immutable

tuple

EX:

let tuple_text:(i32,bool,bool,f32,char)=(1,true,false,4.23,'S');

Note: to assign a tuple to another ,they should be the same type tho.

Ex:

let tuple_text:(i32)=(1) != let tuple_text:(i32)=(1)

they aren't the same


acess tuple :

  • YOU CAN'T ACESS THEM ALL !!!!
  • so , instead use the index

    EX:

    let tup:(i32,char)=(3,'d');
    println("{}",tup.0);
    //output: 3
    

array:

Note: you have to specify the [type;size] & they have to be the same type .

EX:

let arr:[i32;4]=[1,2,3,4];

Note: access elements by there Indices. && can't add more elements to the array, i have to create a new array


  • if i need to change any of these values i should make them mutable by putting( mut ) firest then assign the new value

EX:

let mut text:String ="Hi, MOM!";
text:String="Hi, DAD!";

LOL that's it . thank you

Did you find this article valuable?

Support Hamdy Saad by becoming a sponsor. Any amount is appreciated!