Mutable variable not mutating

When I run this, if the values c, C, f, or F are input the loop correctly breaks and subsequent code executes. However if invalid input is found the loop repeats as expected, but the mutable direction variable is never updated with the new input so the loop executes indefinitely. It is acting as if it is not mutable and is allowing only 1 assignment. No errors or warnings are thrown at compile time. Any ideas would be appreciated. Thanks.

fn main() {
    let mut direction = String::new();
    loop {
        
        println!("Enter C to convert Celsius to Farenheit");
        println!("Enter F to convert Farenheit to Celsius");
        
        io::stdin()
        .read_line(&mut direction)
        .expect("Failed to read line");
        println!("direction = {}", direction);
        if (direction.trim().to_uppercase() == "C") || (direction.trim().to_uppercase() == "F"){
            break;
        }
    }
//subsequent code
}

read_line appends to the string, so on the second iteration it contains something like "F\nF"

1 Like

Got it!. Thank you Alice.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.