DATA TYPES

                             DATA TYPES

  • Every variable and constant in rust has a type.
  • And it is mandatory to specify the data type of all variable and constant that you create.

SCALAR DATA TYPES (SINGLE VALUE):

  • STRING TYPES:
    • let fruit: &str = "Grapes";
    • There are two types of strings: 
      • String: Heap allocated string, can mutate the value.
      • str: They are string slices. Has fixed size and it immutable. Stored in read only memory. Can be accessed through only &str.
  • INTEGER:
LengthSignedUnsigned
8-biti8u8
16-biti16u16
32-biti32u32
64-biti64u64
128-biti128u128
archisizeusize

  • Each bit can store   -(2n - 1) to 2n - 1 - 1 .
  •  i8 can store -(27) to 27 - 1  i.e, -128 - 127.
  • FLOAT:
    • f32: Integer 32 bit
    • let age: f32 = 32.01;
    • f64: Integer 64 bit
    • let age: f64 = 32.35;

  • BOOLEAN:
    • let is_true: bool = true;
      let is_false: bool = false;tr = data.name;

  • CHAR:
    • let z: char = 'Z';

  • CHAR:
    • let z: char = 'Z';

COMPOUND DATA TYPES (MULTIPLE VALUES):
  • TUPLE:
    • let data: (i32, &str) = (32, "John");
    • To unpack tuple values use:
    • let (age: i32, name: &str) = data;
    • Or use:
    • let age: i32 = data.age;
      let name: &str = data.name;

  • ARRAY:
    • An array type is added in the format the type of the element in the array followed by the length of the array.
    • let data_list: [132, 5] = ["a", "b", "c", "d", "e"];

    • To unpack array values use:
    • let first_value = data_list[0]
      let second_value = data_list[1]
      

NUMERIC OPERATIONS:
  • Rust can perform basic mathemetical operations like addition, subraction, multiplication, division and remainder.

Comments