Guessing game in The book

Hello everyone. I started to read "The Book" to learn rust here a problem I've encounter. I'm no programmer and try many thing in the code to fix it myself but to no avail. If someone had the kindness to explain me what is my mistake I would kindly appreciate thank you.

Here is a picture of what the problem seems to be.

Here is rustc version:
rustc 1.49.0 (e1884a8e3 2020-12-29)

Welcome! It looks like you're trying to parse a number out of the string guess before you read any input into it. Try moving line 16 after lines 18-20.

(The source of the problem would probably be more obvious if this code didn't use the same name, guess, for both the string holding the user input and the parsed number. Re-using a variable name like this is called "shadowing", and Rust allows it, but it can make things less clear in cases like this.)

In the future, please post code and compiler errors as text inside Markdown code blocks (a line with three backticks in a row, ```, followed by your code/error, followed by another line of three backticks), not as images. This is easier for everyone to read and is accessible to people using screen readers.

3 Likes

There's online documentation for standard library types and methods. You can read the type (and even the source code) of the method stdin().read_line() – it is apparent that it takes a &mut String as its argument. Usually, there is also example code that you can follow in order to understand the usage of the function in context.

Thank you both for your answers moving the line did indeed work, what a stupid mistake from my part.
I will try to read more of the documentation, still is a bit complicated to understand for me i guess.

1 Like

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.