๊ด€๋ฆฌ ๋ฉ”๋‰ด

IT’s Portfolio

[Rust] Start Rust (Day 4) - Guessing Game [3] ๋ณธ๋ฌธ

Development Study/Rust

[Rust] Start Rust (Day 4) - Guessing Game [3]

f1r3_r41n 2022. 10. 27. 13:41
728x90
๋ฐ˜์‘ํ˜•

๐Ÿฆ€ Rust Day 4

์ˆซ์ž ๋งž์ถ”๊ธฐ ๊ฒŒ์ž„ ๊ตฌํ˜„ ํŒŒํŠธ์˜ ๋ฐ˜๋ณต๋ฌธ

Code

use std::io;
use std::cmp::Ordering;
use rand::Rng;

fn main(){
    println!("Guess the number!");

    let secret_number = rand::thread_rng().gen_range(1, 101);
    println!("The number of player must guess: {}", secret_number);
    println!("The number of player thinks.");

    loop{
        let mut guess = String::new();
        io::stdin().read_line(&mut guess)
            .expect("Unable to read input!");
        let guess: u32 = guess.trim().parse()
            .expect("Input Error!");

        println!("Input Number: {}", guess);

        match guess.cmp(&secret_number){
            Ordering::Less => println!("Your number is Less."),
            Ordering::Greater => println!("Your number is Greater."),
            Ordering::Equal => {
                println!("Correct!!");
                break;
            }
        }
    }
}
  • loop: ๋ฌดํ•œ ๋ฐ˜๋ณต ์‹คํ–‰
    • ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ๊ฐ’์„ ๋ฌป๋Š” ์ฝ”๋“œ๋ฅผ loop ์•ˆ์œผ๋กœ ์˜ฎ๊น€
    • ํ•˜์ง€๋งŒ ๋์—†์ด ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ๊ฐ’์„ ์š”๊ตฌํ•˜๊ฒŒ ๋จ
    • ๋ฌดํ•œ ๋ฃจํ”„์—์„œ ํƒˆ์ถœํ•  ๋ฐฉ๋ฒ•
      • ctrl+c: ํ”„๋กœ๊ทธ๋žจ ๊ฐ•์ œ ์ข…๋ฃŒ
      • ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๊ฐ’ ์ž…๋ ฅ: ์—๋Ÿฌ๋กœ ์ธํ•œ ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ
    • ์ˆซ์ž๋ฅผ ๋งž์ถ”๋ฉด ์ข…๋ฃŒํ•˜๊ธฐ ์œ„ํ•ด Ordering::Equal => ์ฝ”๋“œ์— break ๊ตฌ๋ฌธ ์ถ”๊ฐ€

์ˆซ์ž ๋งž์ถ”๊ธฐ ๊ฒŒ์ž„ ๊ตฌํ˜„ ํŒŒํŠธ์˜ ์˜ค์ž…๋ ฅ ์ฒ˜๋ฆฌ

Code

use core::num;
use std::io;
use std::cmp::Ordering;
use rand::Rng;

fn main(){
    println!("Guess the number!");

    let secret_number = rand::thread_rng().gen_range(1, 101);
    println!("The number of player must guess: {}", secret_number);
    println!("The number of player thinks.");

    loop{
        let mut guess = String::new();
        io::stdin().read_line(&mut guess)
            .expect("Unable to read input!");
        let guess: u32 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => continue,
        };

        println!("Input Number: {}", guess);

        match guess.cmp(&secret_number){
            Ordering::Less => println!("Your number is Less."),
            Ordering::Greater => println!("Your number is Greater."),
            Ordering::Equal => {
                println!("Correct!!");
                break;
            }
        }
    }
}
  • ์‚ฌ์šฉ์ž๊ฐ€ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ–ˆ์„ ๋•Œ ํ”„๋กœ๊ทธ๋žจ์„ ๊ทธ๋ƒฅ ์ข…๋ฃŒํ•˜๋Š” ๋Œ€์‹  ๋งค๋„๋Ÿฝ๊ฒŒ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•จ
    • ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๊ฐ’ ์ž…๋ ฅ -> ๋ฌด์‹œ -> ์žฌ์ž…๋ ฅ
  • guess ๋ณ€์ˆ˜๋ฅผ ํ˜• ๋ณ€ํ™˜ํ•˜๋Š” ์ฝ”๋“œ ์ˆ˜์ •
    •     let guess: u32 = guess.trim().parse()
              .expect("Input Error!!");
          => let guess: u32 = match guess.trim().parse() {
              Ok(num) => num,
              Err(_) => continue,
          };
      • expect ๋ฉ”์„œ๋“œ -> match ํ‘œํ˜„์‹
  • parse ๋ฉ”์„œ๋“œ
    • ๋ฌธ์ž์—ด์„ ์ˆซ์ž๋กœ ๋ฌธ์ œ์—†์ด ๋ณ€ํ™˜ํ•˜๋ฉด ๋ณ€ํ™˜๋œ ์ˆซ์ž๋ฅผ ๊ฐ€์ง„ Ok ๊ฐ’ ๋ฆฌํ„ด
    • ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•˜์ง€ ๋ชปํ•˜๋ฉด ๊ด€๋ จ๋œ ์—๋Ÿฌ ์ •๋ณด๋ฅผ ๋‹ด์€ Err ๊ฐ’ ๋ฆฌํ„ด
  • _: ๋ชจ๋“  ๊ฐ’์„ ํ‘œํ˜„ํ•˜๋Š” ๋ฌธ์ž

Final Code

use std::io;
use std::cmp::Ordering;
use rand::Rng;

fn main(){
    println!("Guess the number!");

    let secret_number = rand::thread_rng().gen_range(1, 101);

    loop{
        println!("The number of player thinks.");

        let mut guess = String::new();
        io::stdin().read_line(&mut guess)
            .expect("Unable to read input!");
        let guess: u32 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => continue,
        };

        println!("Input Number: {}", guess);

        match guess.cmp(&secret_number){
            Ordering::Less => println!("Your number is Less."),
            Ordering::Greater => println!("Your number is Greater."),
            Ordering::Equal => {
                println!("Correct!!");
                break;
            }
        }
    }
}

728x90
๋ฐ˜์‘ํ˜•
Comments