Back to articles
[Rust Guide] 2.3. Number Guessing Game Pt.3 - Comparing Input and Random Number

[Rust Guide] 2.3. Number Guessing Game Pt.3 - Comparing Input and Random Number

via Dev.toSomeB1oody

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

Read Full Article
2 views

Related Articles