if Expressions

202307071943
Status: #idea
Tags: Rust

if Expressions

Info

Condition must be a boolean. Rust does NOT auto convert non-booleans to booleans

let n = 3;
;
if n > 5 {
	println!("true");
} else {
	println!("false");
}

Using if in a let statement

let number = if condition { 5 } else { 6 };

let number = if condition { 5 } else { "six" }; // Error

References

  1. https://rust-book.cs.brown.edu/ch03-05-control-flow.html#if-expressions
  2. Control Flow