In Rust, why do we need to specify a reference variable is mutable each time we change its value?

114 views Asked by At

In the following guessing game, from the Rust Book, why do we have to say that the reference to the variable .read_line(&mut guess) is mutable since we already stated it was mutable in a previous line let mut guess = String::new();?

use std::io; 

fn main() { 
    println!("Guess the number!"); 

    println!("Please input your guess."); 

    let mut guess = String::new(); 

    io::stdin() 
        .read_line(&mut guess)
        .expect("Failed to read line"); 

    println!("You guessed: [guess]"); 
} 
0

There are 0 answers