์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- java
- ํ์ด์ฌ
- C
- ๋ฐ์ดํฐ๋ฒ ์ด์ค
- ๋ฌ์คํธ ์์
- ๋ฌ์คํธ
- ๋ฐ์ดํฐ ํต์
- Python
- ์ด์์ฒด์
- Operating System
- ubuntu
- ํ์ด์ฌ ์๊ณ ๋ฆฌ์ฆ
- ์๋ฐ ๊ฐ๋
- data communication
- Database
- ๋ฐฑ์ค ๋ฌ์คํธ
- OS
- ๋ฐฑ์ค
- ์๋ฐ ๊ธฐ์ด
- Rust
- ๋ฌ์คํธ ํ๋ก๊ทธ๋๋ฐ ๊ณต์ ๊ฐ์ด๋
- ์ฐ๋ถํฌ
- Python challenge
- Reversing
- ์๋ฐ
- ์๊ณ ๋ฆฌ์ฆ
- ํ์ด์ฌ ์ฒผ๋ฆฐ์ง
- ์ค๋ผํด
- ์ค๋ผํดDB
- ํ์ด์ฌ ์ฑ๋ฆฐ์ง
Archives
- Today
- Total
IT’s Portfolio
[Rust] Start Rust (Day 4) - Guessing Game [3] ๋ณธ๋ฌธ
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
๋ฐ์ํ
'Development Study > Rust' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Rust] Start Rust (Day 6) - Common Programming Concepts [2] (2) | 2023.01.03 |
---|---|
[Rust] Start Rust (Day 5) - Common Programming Concepts [1] (0) | 2023.01.03 |
[Rust] Start Rust (Day 3) - Guessing Game [2] (0) | 2022.08.23 |
[Rust] Start Rust (Day 2) - Guessing Game [1] (0) | 2022.08.09 |
[Rust] Start Rust (Day 1) - Start Step (0) | 2022.08.07 |
Comments