Programming Concepts
Variables and Mutability
fn main() {
let x = 5;
println!("The value of x is: {}", x);
x = 6;
println!("The value of x is: {}", x);
}️⛔
Error: cannot mutate immutable variable x
- because by default Rust variables are immutable
fn main() {
let mut x = 5;
println!("The value of x is: {}", x);
x = 6;
println!("The value of x is: {}", x);
}- adding
mutmakes the variable mutable
Constants
fn main() {
const MAX_POINTS: u32 = 100_000;
}- Constant variables must be annotated with a type, and it is not inferred with a type at compile time
fn main() {
const mut MAX_POINTS: u32 = 100_000;
}️⛔
Error: const globals cannot be mutable
Shadowing
fn main() {
let x = 5;
println!("The value of x is: {}", x);
let x = 6;
println!("The value of x is: {}", x);
}
xin line 2 is shadowed byxin line 5- helps in preserving immutability
fn main() {
let x = 5;
println!("The value of x is: {}", x);
let x = "five";
println!("The value of x is: {}", x);
}- changing the type of the variable
Scalar Datatypes
- represent a single value (integers, floating-point numbers, Booleans, and characters)
fn main() {
let x: u8 = 255;
let y: u16 = 65535;
let z: u32 = 4_294_967_295;
let a: u64 = 18_446_744_073_709_551_615;
let b: u128 = 340_282_366_920_938_463_463_374_607_431_768_211_455;
let c: usize = usize::MAX;
let d: i8 = 127;
let e: i16 = 32_767;
let f: i32 = 2_147_483_647;
let g: i64 = 9_223_372_036_854_775_807;
let h: i128 = 170_141_183_460_469_231_731_687_303_715_884_105_727;
let i: isize = isize::MAX;
let j: f32 = f32::MAX;
let k: f64 = f64::MAX;
let l:bool = true;
let m:char = 'a';
let m:char = '😻';
}https://doc.rust-lang.org/book/ch03-02-data-types.html (opens in a new tab)
fn main() {
let f: u8 = 255;
println!("The value of f is: {}", f);
let f: u8 = 256;
println!("The value of f is: {}", f);
}️⛔
Error: literal out of range for u8
Tuple
fn main() {
let tup: (i32, f64, u8) = (500, 6.4, 1);
}- tuple is a collection of multiple values of different types
Destructuring Tuple
- destructuring tuple to get individual values
- or using dot notation to get individual values
fn main() {
let tup = (500, 6.4, 1);
let (x, y, z) = tup;
let x = tup.0;
}Array
- Array is a collection of multiple values of the same type and fixed length
fn main() {
let a = [1, 2, 3, 4, 5];
}Functions
- Functions are defined using
fnkeyword - Function names follow the snake_case convention
fn main() {
print_hello(1000);
}
fn print_hello(number: u16) {
println!("Hello, {}", number);
}Statements and Expressions
- Statements are instructions that perform some action and do not return a value
- Expressions evaluate to a resulting value
Returning Values from Functions
- return values using
->arrow notation - The last expression in the function is the return value
fn main() {
let x = five();
println!("The value of x is: {}", x);
}
fn five() -> i32 {
5
}Control Flow
ifexpression must be explicitlybooltype
fn main() {
let number = 3;
if number < 5 {
println!("condition was true");
} else {
println!("condition was false");
}
}Loops
loopkeyword is used to create an infinite loopbreakkeyword is used to break the loop
fn main() {
let mut counter = 0;
loop {
counter += 1;
if counter == 10 {
break;
}
}
}While Loop
whileloop is used to loop until a condition is met
fn main() {
let mut number = 3;
while number != 0 {
println!("{}!", number);
number -= 1;
}
println!("LIFTOFF!!!");
}For Loop
forloop is used to loop over a collectionitermethod is used to create an iterator over a collection
fn main() {
let a = [10, 20, 30, 40, 50];
for element in a.iter() {
println!("the value is: {}", element);
}
}For Loop with Range
forloop is used to loop over a rangerevmethod is used to reverse the range
fn main() {
for number in (1..4).rev() {
println!("{}!", number);
}
println!("LIFTOFF!!!");
}