Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Conditional (Ternary) operator

  • ?:
  • if else
fn main() {
    let b = true;
    let answer = if b { 1 } else { 2 };
    println!("{answer}");

    let b = false;
    let answer = if b { 1 } else { 2 };
    println!("{answer}");
}
1
2