Skip to content

Fix missing input parsing in Chapter 7 guessing game example - #4855

Open
Sha1rholder wants to merge 1 commit into
rust-lang:mainfrom
Sha1rholder:fix/ch07-guessing-game-type-mismatch
Open

Sha1rholder wants to merge 1 commit into
rust-lang:mainfrom
Sha1rholder:fix/ch07-guessing-game-type-mismatch

Conversation

@Sha1rholder

Copy link
Copy Markdown

The Chapter 7 example in
listings/ch07-managing-growing-projects/no-listing-01-use-std-unnested/src/main.rs fails to compile with E0308 because guess is a String while secret_number is an integer.

This is the same type mismatch reported in #3562. In Chapter 2, Listing 2-4 intentionally does not compile, and the following explanation introduces the missing conversion. The Chapter 7 example carries over that incomplete code, where the omission no longer serves that teaching purpose and should be fixed.

Parse the trimmed input as u32 before comparing it with the secret number, using the conversion already introduced in Chapter 2.

The change is outside the here anchor, so the displayed use statements remain unchanged.

Validation:

  • Confirmed the original example fails cargo check with E0308
  • Confirmed the updated example passes cargo check

Parse the guess as u32 before comparing it with the secret number.

The example reuses the incomplete code from Listing 2-4 without the
conversion needed to compile.

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

1 participant