Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guessing Game (chapter 2) suggestion #111

Open
pdavidow opened this issue Jul 17, 2023 · 1 comment
Open

Guessing Game (chapter 2) suggestion #111

pdavidow opened this issue Jul 17, 2023 · 1 comment

Comments

@pdavidow
Copy link

pdavidow commented Jul 17, 2023

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();
@pdavidow pdavidow changed the title Guessing Game question Guessing Game suggestion Jul 17, 2023
@pdavidow pdavidow changed the title Guessing Game suggestion Guessing Game (chapter 2) suggestion Jul 17, 2023
@willcrichton
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants