Primitive Data Types in Rust

Money Grows on Trees
2 min readJun 15, 2020

char - holds a character; size : 4 bytes (it holds unicode scalar values and each is 21-bits long)

let a: char = 'a'; // "" is used for string slices

bool - true or false; size: size : 1 byte

let a: bool = true;
let b = false;

integer - can be signed or unsigned; can customize the size of the integer

// size of an integer can range from 8 to 128 bits
let a = 5; // by default it is i32
let b: u64 = 5; // unsigned integer with size 64 bits
/* we can also use isize and usize to describe the sign and size of an integer; basically i or u represents the sign and size is how many bytes it takes to reference any location in memory - they are called pointer-sized integer types */let e: isize = 10; // will be signed integer of size 32 bits on 32-bit machine
let f: usize = 10; // will be an unsigned integer of size 64 bits on a 64-bit machine
When accessing an index of an array, it is a good practice to use unsigned integer type u{int_size}

float - decimals; 2 types : f32 & f64 (f64 has double precision)

let x = 1.0; // default type is f64
let y: f32 = 3.14; // type is f32

Array - collection of multiple values; each value must be of the same type; fixed length

/* arrays are stored on the stack; if you want dynamic sized arrays, look into vectors */let a = [1, 2, 3, 4];let b = [5; 3]; // b = [5, 5, 5]let c: [i64; 2] = [1, 2];// Accessing array elements
let first_index = a[0];
let invalid_index = a[10]; // this will compile but throw runtime error

Tuple - can group different types of values together into one collection; fixed length

let tup: (i32, f64, u8) = (1, 2.3, 5);// we can access the values in a tuple by using pattern matching
let (a, b, c) = tup;
println!("a: {}, b: {}, c: {}", a, b, c);
// or we can use '.' followed by the index of the value we want to access on the tuple variablelet first_val = tup.0;
let second_val = tup.1;
println!("First val: {}, second val: {}", first_val, second_val);

--

--