cognitive-engineering-lab / rust-book

The Rust Programming Language: Experimental Edition
https://rust-book.cs.brown.edu
Other
504 stars 82 forks source link

Guessing Game (chapter 2) suggestion #111

Open pdavidow opened 11 months ago

pdavidow commented 11 months ago

Why not just declare guess once, like this:

    let mut guess;
    loop {
        guess = String::new();

instead of what's presented:

    loop {
        let mut guess = String::new();
willcrichton commented 11 months ago

That's a valid alternative. I didn't write this chapter, but if I had to guess at the intent, it's that it's better to declare variables as close as possible to their point of use. Declaring a single mutable variable would probably not affect the performance of the program, so it's a largely stylistic choice.