Misc

Variable Scope

{
  // The scope limited to this braces
  let a\_number = 1;
}
println!("{a\_number}");

This will produce error as the scope of the variable a_number ends at the braces

De-referencing

let mut borrow = 10;
let deref = &mut borrow;
println!("{}", *deref);

De-referencing in rust can be done using the * operator

Borrowing

let mut foo = 4;
let mut borrowed\_foo = &foo;
println!("{borrowed\_foo}");

let mut bar = 3;
let mut mutable\_borrowed\_bar = &mut bar;
println!("{mutable\_borrowed\_bar}");

Here borrowed value borrows the value from value one using & operator.

Type Casting

let a\_int = 90; // int
// int to float
let mut type\_cast = (a_int as f64);

let orginal: char = 'I';
// char to int => 73
let type\_casted: i64 = orginal as i64;

To perform type-casting in Rust one must use the as keyword.

Comments