![[Rust Guide] 2.3. Number Guessing Game Pt.3 - Comparing Input and Random Number](/_next/image?url=https%3A%2F%2Fmedia2.dev.to%2Fdynamic%2Fimage%2Fwidth%3D800%252Cheight%3D%252Cfit%3Dscale-down%252Cgravity%3Dauto%252Cformat%3Dauto%2Fhttps%253A%252F%252Fdev-to-uploads.s3.amazonaws.com%252Fuploads%252Farticles%252F5zvsmmwgpm5gzo5c8q3t.png&w=1200&q=75)
[Rust Guide] 2.3. Number Guessing Game Pt.3 - Comparing Input and Random Number
2.3.0. Key Points of This Section In this section, you will learn: How to use match Shadowing Type casting The Ordering type 2.3.1. Game Objectives Generate a random number between 1 and 100 Prompt the player to enter a guess After the guess, the program will tell whether the guess is too big or too small (covered in this section) If the guess is correct, print a congratulatory message and exit the program 2.3.2. Code Implementation This is the code written up to the previous article: use std :: io ; use rand :: Rng ; fn main () { let range_number = rand :: thread_rng () .gen_range ( 1 .. 101 ); println! ( "Number Guessing Game" ); println! ( "Guess a number" ); let mut guess = String :: new (); io :: stdin () .read_line ( & mut guess ) .expect ( "Could not read the line" ); println! ( "The number you guessed is:{}" , guess ); println! ( "The secret number is: {}" , range_number ); } Step 1: Data Type Conversion From the code, we can see that the variable guess is of string type, while
Continue reading on Dev.to
Opens in a new tab



