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:
Length Signed Unsigned 8-bit i8u816-bit i16u1632-bit i32u3264-bit i64u64128-bit i128u128arch isizeusize
- 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;
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.
| Length | Signed | Unsigned |
|---|---|---|
| 8-bit | i8 | u8 |
| 16-bit | i16 | u16 |
| 32-bit | i32 | u32 |
| 64-bit | i64 | u64 |
| 128-bit | i128 | u128 |
| arch | isize | usize |
- 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;
let is_true: bool = true; let is_false: bool = false;tr = data.name;
- CHAR:
let z: char = 'Z';
let z: char = 'Z';
- CHAR:
let z: char = 'Z';
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;
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"];
- 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]
- 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.
- Rust can perform basic mathemetical operations like addition, subraction, multiplication, division and remainder.
Comments
Post a Comment