Function if_while_true

Source
pub fn if_while_true()
Expand description

§If True && While True

fn if_true(x: i32) -> i32 {
    if true {  // error[E0308]: mismatched types,expected type `i32` found type `()`
        return x+1;
    }
}

fn while_true(x: i32) -> i32 {
    while true {  // error[E0308]: mismatched types,expected type `i32` found type `()`
        return x+1;
    }
}

fn main() {
    let y = while_true(5);
    assert_eq!(y, 6);

    let y = if_true(5);
    assert_eq!(y, 6);

    let x;
    // while true { x = 1; break; }
    loop { x = 1; break; }
    println!("{}", x);

}