logo

Rust Cheat Sheet

Published on

Pratical Rust Cheat Sheet

1. Hello World

fn main() {
    println!("Hello World!");
}

1.1 Comments

// Line Comments
/*.............Block Comments */
/// Outer doc comments
//! Inner doc comments

Details

1.2 Formatted Print

// Single Placeholder
println!("{}", 1);

// Multiple Placeholder
println!("{} {}", 1, 3);

// Positional Arguments
println!("{0} is {1} {2}, also {0} is a {3} programming language", "Rust", "cool", "language", "safe");

// Named Arguments
println!("{country} is a diverse nation with unity.", country = "India");

// Placeholder traits :b for binary, :0x is for hex and :o is octal
println!("Let us print 76 is binary which is {:b} , and hex equivalent is {:0x} and octal equivalent is {:o}", 76, 76, 76);

// Debug Trait
println!("Print whatever we want to here using debug trait {:?}", (76, 'A', 90));

// New Format Strings in 1.58
let x = "world";
println!("Hello {x}!");

2. Data Types

  • Signed integers: i8, i16, i32, i64, i128 and isize
  • Unsigned integers: u8, u16, u32, u64, u128 and usize
  • Floating point: f32, f64
  • char: Unicode scalar values (4 bytes each)
  • bool
  • unit type (), whose only possible value is empty ()
  • Arrays like [1, 2, 3]
  • Tuples like (1, true)
// Slices
let mut array: [ i64; 4] = [1,2,3,4];
let mut slices: &[i64] = &array[0..3] // Lower range is inclusive and upper range is exclusive

println!("The elements of the slices are : {slices:?}");

3. Strings

let cs:&str = "cheat sheet";

let my_string = String::new();

// Converting to a string object
let S_string = a_string.to_string()

// Creating an initialized string object
let lang = String::from("Rust");

let name = String::from("ElementalX");
name.contains("Element") // => true

let mut half_text = String::from("Hal");
half_text.push('f');    // => Half

let mut hi = String::from("Hey there...");
hi.push_str("How are you doing??");

// => Hey there...How are you doing??
println!("{hi}");

4. Flow Control

4.1 If ... Else if ... Else ...

let foo = 12;
let bar = 13;

if foo == bar {
  println!("foo is equal to bar");
} else if foo < bar {
  println!("foo less than bar");
} else if foo != bar {
  println!("foo is not equal to bar");
} else {
  println!("Nothing");
}

4.2 If ... Let

let mut arr1:[i64 ; 3] = [1,2,3];
if let[1,2,_] = arr1{
  println!("Works with array");
}

let mut arr2:[&str; 2] = ["one", "two"];
if let["Apple", _] = arr2{
  println!("Works with str array too");
}

4.3 Match Expression

let day_of_week = 2;

match day_of_week {
  1 => {
    println!("Its Monday my dudes");
  },
  2 => {
    println!("It's Tuesday my dudes");
  },
  _ => {
    println!("Default!")
  }
};

4.4 Loop

let mut i = 1;

loop {
  println!("i is {i}");
  if i > 100 {
    break;
  }

  if i > 50 {
    continue;
  }

  i *= 2;
}

for mut i in 0..15 {
  println!("The value of i is : {i}");
}

let mut check =  0;
while check < 11{
  println!("Check is : {check}");
  check+=1;
  println!("After incrementing: {check}");

  if check == 10{
    break; // stop while
  }
}