Control Flows

if - else if - else

  • Using only if block.
let age = 13;

if age < 18 {
    println!("Hello, child!"); // The code prints this
}
  • Using only if and else blocks.
let i = 7;

if i % 2 == 0 {
    println!("Even");
} else {
    println!("Odd"); // The code prints this
}
  • Using with let statement.
let age: u8 = 13;
let is_below_eighteen = if age < 18 { true } else { false }; // true
  • More examples,
// i. A simple example
let team_size = 7;

if team_size < 5 {
    println!("Small");
} else if team_size < 10 {
    println!("Medium"); // The code prints this
} else {
    println!("Large");
}
// ii. Let's refactor above code
let team_size = 7;
let team_size_in_text;

if team_size < 5 {
    team_size_in_text = "Small";
} else if team_size < 10 {
    team_size_in_text = "Medium";
} else {
    team_size_in_text = "Large";
}

println!("Current team size : {}", team_size_in_text); // Current team size : Medium
// iii. Let's refactor further
let team_size = 7;
let team_size = if team_size < 5 {
    "Small" // ⭐️ no ;
} else if team_size < 10 {
    "Medium"
} else {
    "Large"
};

println!("Current team size : {}", team_size); // Current team size : Medium

⭐️ Return data type should be the same on each block when using this as an expression.

match

let tshirt_width = 20;
let tshirt_size = match tshirt_width {
    16 => "S", // check 16
    17 | 18 => "M", // check 17 and 18
    19 ..= 21 => "L", // check from 19 to 21 (19,20,21)
    22 => "XL",
    _ => "Not Available",
};

println!("{}", tshirt_size); // L
let is_allowed = false;
let list_type = match is_allowed {
    true => "Full",
    false => "Restricted"
    // no default/ _ condition can be skipped
    // Because data type of is_allowed is boolean and all possibilities checked on conditions
};

println!("{}", list_type); // Restricted
let marks_paper_a: u8 = 25;
let marks_paper_b: u8 = 30;

let output = match (marks_paper_a, marks_paper_b) {
    (50, 50) => "Full marks for both papers",
    (50, _) => "Full marks for paper A",
    (_, 50) => "Full marks for paper B",
    (x, y) if x > 25 && y > 25 => "Good",
    (_, _) => "Work hard"
};

println!("{}", output); // Work hard

loop

loop {
	println!("Loop forever!");
}
// Usage of break and continue
let mut a = 0;

loop {
	if a == 0 {
		println!("Skip Value : {}", a);
		a += 1;
		continue;
	} else if a == 2 {
		println!("Break At : {}", a);
		break;
	}

	println!("Current Value : {}", a);
	a += 1;
}
// Outer break
let mut b1 = 1;

'outer_loop: loop { //set label outer_loop
  let mut b2 = 1;

  'inner_loop: loop {
    println!("Current Value : [{}][{}]", b1, b2);

    if b1 == 2 && b2 == 2 {
        break 'outer_loop; // kill outer_loop
    } else if b2 == 5 {
    	break;
    }

    b2 += 1;
  }

  b1 += 1;
}

while

let mut a = 1;

while a <= 10 {
	println!("Current value : {}", a);
	a += 1; //no ++ or -- on Rust
}
// Usage of break and continue
let mut b = 0;

while b < 5 {
	if b == 0 {
		println!("Skip value : {}", b);
		b += 1;
		continue;
	} else if b == 2 {
		println!("Break At : {}", b);
		break;
	}

	println!("Current value : {}", b);
	b += 1;
}
// Outer break
let mut c1 = 1;

'outer_while: while c1 < 6 { //set label outer_while
	let mut c2 = 1;

	'inner_while: while c2 < 6 {
		println!("Current Value : [{}][{}]", c1, c2);
		if c1 == 2 && c2 == 2 { break 'outer_while; } //kill outer_while
		c2 += 1;
	}

	c1 += 1;
}

for

// 0 to 10 (10 exclusive); In other languages, `for(i = 0; i < 10; i++)`
for i in 0..10 {
  println!("Current value : {}", i);
}
// 1 to 10 (10 inclusive); In other languages, `for(i = 1; i <= 10; i++)`
for i in 1..=10 {
  println!("Current value : {}", i);
}
// Usage of break and continue
for b in 0..6 {
  if b == 0 {
    println!("Skip Value : {}", b);
    continue;
  } else if b == 2 {
    println!("Break At : {}", b);
    break;
  }

  println!("Current value : {}", b);
}
// Outer break
'outer_for: for c1 in 1..6 { //set label outer_for

  'inner_for: for c2 in 1..6 {
    println!("Current Value : [{}][{}]", c1, c2);
    if c1 == 2 && c2 == 2 { break 'outer_for; } //kill outer_for
  }

}
// Working with arrays/vectors
let group : [&str; 4] = ["Mark", "Larry", "Bill", "Steve"];

for n in 0..group.len() { // group.len() = 4 -> 0..4 👎 check group.len() on each iteration
  println!("Current Person : {}", group[n]);
}

for person in group.iter() { // 👍 group.iter() turn the array into a simple iterator
  println!("Current Person : {}", person);
}

for (index, person) in group.iter().enumerate() { // 💡 group.iter().enumerate() helps to read both the current index (starting from zero) and the value
  println!("Person {} : {}", index, person);
}