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

IT’s Portfolio

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

Development Study/Rust

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

f1r3_r41n 2022. 8. 23. 16:24
728x90
๋ฐ˜์‘ํ˜•

๐Ÿฆ€ Rust Day 3

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

rand crate

  • ๋‚œ์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” rand๋ผ๋Š” crate๊ฐ€ ํ•„์š”ํ•จ
  • crate: ์†Œ์Šค ํŒŒ์ผ์˜ ์ง‘ํ•ฉ
    • ๋Ÿฌ์ŠคํŠธ์˜ ๊ธฐ๋ณธ ํŒจํ‚ค์ง€, ํ˜น์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋‹จ์œ„
    • ์šฐ๋ฆฌ๊ฐ€ ์ž‘์—… ์ค‘์ธ ํ”„๋กœ์ ํŠธ๋„ ์‹คํ–‰์ด ๊ฐ€๋Šฅํ•œ binary crate
    • ํŒŒ์ด์ฌ์˜ Library ๊ฐœ๋…์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ์ดํ•ดํ•˜๊ธฐ ํŽธํ•  ๋“ฏ
  • Cargo.toml ํŒŒ์ผ์„ ์ˆ˜์ •ํ•˜์—ฌ ์˜์กด ํŒจํ‚ค์ง€๋กœ ๋“ฑ๋กํ•ด์•ผ ํ•จ
    • ์˜์กด ํŒจํ‚ค์ง€ ์ถ”๊ฐ€ ์‹œ Cargo๋Š” ํŒจํ‚ค์ง€๊ฐ€ ๋“ฑ๋ก๋œ repository์ธ https://crates.io์—์„œ ์ตœ์‹  ๋ณต์‚ฌ๋ณธ์„ ๋‚ด๋ ค๋ฐ›์Œ
      • https://crates.io: ๋Ÿฌ์ŠคํŠธ ๊ฐœ๋ฐœ์ž๋“ค์˜ ์˜คํ”ˆ ์†Œ์Šค ํ”„๋กœ์ ํŠธ ์ €์žฅ์†Œ

Cargo.lock

  • ์ตœ์ดˆ cargo build ๋ช…๋ น ์‹คํ–‰ ์‹œ ์ƒ์„ฑ
  • ๋Ÿฌ์ŠคํŠธ๋Š” lock ํŒŒ์ผ์˜ crate ๋ฒ„์ „์„ ๋ฒ ์ด์Šค๋กœ ์‹คํ–‰ํ•จ
  • Cargo์˜ update ๋ช…๋ น์€ lock ํŒŒ์ผ์— ๋ช…์‹œ๋œ ๋ฒ„์ „์„ ๋ฌด์‹œํ•˜๊ณ  toml ํŒŒ์ผ์— ์ง€์ •๋œ ์กฐ๊ฑด์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ€์žฅ ์ตœ์‹  ๋ฒ„์ „์„ ๋‹ค์‹œ ์ฐพ์Œ. ์„ฑ๊ณต์ ์œผ๋กœ ์‹คํ–‰ ์‹œ lock ํŒŒ์ผ์˜ ๋ฒ„์ „์„ ๊ฐฑ์‹ ํ•จ

Code

use std::io;
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.");

    let mut guess = String::new();
    io::stdin().read_line(&mut guess)
        .expect("Unable to read input!");

    println!("Input Number: {}", guess);
}
  • use rand::Rng: Rng trait๋Š” ๋‚œ์ˆ˜ ์ƒ์„ฑ๊ธฐ์— ๊ตฌํ˜„๋œ ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•จ
    • trait: ๋ถˆํŠน์ • ํƒ€์ž…์„ ๋Œ€์ƒ์œผ๋กœ ์ด๋“ค์ด ๊ตฌํ˜„ํ•ด์•ผ ํ•  ๋ฉ”์„œ๋“œ์˜ ์ง‘ํ•ฉ์„ ์ •์˜
  • rand::thread_rng(): ๋‚œ์ˆ˜ ์ƒ์„ฑ๊ธฐ ๋ฐ˜ํ™˜
    • ํ˜„ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ ์ค‘์ธ ์Šค๋ ˆ๋“œ ๋‚ด์— ์กด์žฌํ•˜๋ฉฐ OS๊ฐ€ ์ง€์ •ํ•œ seed ๊ฐ’์„ ์‚ฌ์šฉ
  • gen_range(1, 101): Rng trait์— ์ •์˜๋˜์–ด ์žˆ์Œ. ์ธ์ˆ˜๋กœ ์ง€์ •๋œ ๋‘ ๊ฐ’ ์‚ฌ์ด์˜ ๋‚œ์ˆ˜๋ฅผ ์„ ํƒํ•ด ๋ฐ˜ํ™˜ํ•จ

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

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.");

    // let mut guess = String::new();
    // io::stdin().read_line(&mut guess)
    //     .expect("Unable to read input!");

    // 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!!"),
    }
}
  • std::cmp::Ordering: Order ์—ด๊ฑฐ์ž. Less, Greater, Equal ์ค‘ ํ•˜๋‚˜ ํ‘œํ˜„
    • cmp ๋ฉ”์„œ๋“œ๋Š” ๋‘ ๊ฐœ์˜ ๊ฐ’์„ ๋น„๊ตํ•จ. ๋น„๊ต ๊ฐ€๋Šฅํ•œ ์–ด๋–ค ๊ฐ’์—๋„ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Œ
      • ๋น„๊ต ๊ฒฐ๊ณผ๋ฅผ Ordering ์—ด๊ฑฐ์ž์˜ ์—ด๊ฒƒ๊ฐ’ ์ค‘ ํ•˜๋‚˜ ๋ฐ˜ํ™˜
  • match ํ‘œํ˜„์‹
    • ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ€์ง€๋กœ ๊ตฌ์„ฑ๋จ. ๊ฐ๊ฐ์˜ ๊ฐ€์ง€๋Š” ํŒจํ„ด, ๊ทธ๋ฆฌ๊ณ  match ํ‘œํ˜„์‹์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์— ์ฃผ์–ด์ง„ ๊ฐ’์ด ํŒจํ„ด๊ณผ ์ผ์น˜ํ•  ๋•Œ ์‹คํ–‰๋  ์ฝ”๋“œ๋กœ ๊ตฌ์„ฑ๋จ
    • match ํ‘œํ˜„์‹์— ์ง€์ •๋œ ๊ฐ’์„ ์ฝ์–ด ๊ฐ ๊ฐ€์ง€์˜ ํŒจํ„ด๊ณผ ์ฐจ๋ก€๋Œ€๋กœ ๋น„๊ต
    • C ์–ธ์–ด์˜ switch case ๋ฌธ๊ณผ ๋น„์Šทํ•˜๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ฌ
  • ์‹คํ–‰ ์‹œ ํƒ€์ž… ๋ถˆ์ผ์น˜ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•จ
    • ๋Ÿฌ์ŠคํŠธ๋Š” ์ •์ ์ธ ํƒ€์ž… ์‹œ์Šคํ…œ์„ ๊ฐ–์ท„์ง€๋งŒ ํƒ€์ž… ์ถ”๋ก (type inference)๋„ ์ง€์›ํ•จ
    • ๋•Œ๋ฌธ์— ๋ณ€์ˆ˜ guess๊ฐ€ String ํƒ€์ž…์ด์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์Šค์Šค๋กœ ์ดํ•ดํ•จ
    • ๋ณ€์ˆ˜ secret_number๋Š” ์ˆซ์ž ํƒ€์ž…
      1. 32bit ์ •์ˆ˜ i32: signed 32-bit number
      2. 32bit ๋ถ€ํ˜ธ ์—†๋Š” ์ •์ˆ˜ u32: unsigned 32-bit number
      3. 64bit ์ •์ˆ˜ i64: signed 64-bit number
    • ๋Ÿฌ์ŠคํŠธ๋Š” ์ˆซ์ž ๊ฐ’์— ๋Œ€ํ•ด์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ i32 ํƒ€์ž…์„ ์‚ฌ์šฉํ•˜๋ฉฐ ๋ฌธ์ž์—ด๊ณผ ์ˆซ์ž ํƒ€์ž… ๋น„๊ต๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•จ => ๋ฌธ์ž์—ด๋กœ ์ฝ์€ ๊ฐ’์„ ์‹ค์ œ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•ด์•ผ ๋น„๊ต ๊ฐ€๋Šฅ
    • ๋ณ€ํ™˜ ์ฝ”๋“œ ์ถ”๊ฐ€
      • ๋ณ€์ˆ˜ ๊ฐ€๋ฆฌ๊ธฐ(shadowing): ์–ด๋–ค ํƒ€์ž…์˜ ๊ฐ’์„ ๋‹ค๋ฅธ ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•  ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉ. ๋•Œ๋ฌธ์— ๋‘ ๊ฐœ์˜ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•  ํ•„์š”๊ฐ€ ์—†์Œ
      • trim(): String ํƒ€์ž…์— ์ •์˜๋˜์–ด ์žˆ์Œ. ๋ฌธ์ž์—ด ์–‘์ชฝ ๋์˜ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•จ
      • parse(): ๋ฌธ์ž์—ด์— ์ •์˜๋˜์–ด ์žˆ์Œ. ๋ฌธ์ž์—ด์„ ๊ตฌ๋ฌธ ๋ถ„์„(parsing)ํ•ด์„œ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•จ. ๋‹ค์–‘ํ•œ ํƒ€์ž…์˜ ์ˆซ์ž ๊ตฌ๋ฌธ ๋ถ„์„์ด ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์›ํ•˜๋Š” ์ˆซ์ž ํƒ€์ž…์„ ์ •ํ™•ํžˆ ๋ช…์‹œํ•ด์•ผ ํ•จ
        • Result ํƒ€์ž…์˜ ์—ด๊ฒƒ๊ฐ’(Ok, Err) ์ค‘ ํ•˜๋‚˜๋ฅผ ๋ฐ˜ํ™˜
        • expect() ์‚ฌ์šฉ
    • let guess: u32 = guess.trim().parse().expect("Input Error!");

u32: ์ž‘์€ ๋ฒ”์œ„์˜ ์–‘์ˆ˜๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ์— ์•Œ๋งž์€ ์ˆซ์ž ํƒ€์ž…

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