【rust 猜数字游戏示例代码】

示例代码是从《Rust 程序设计语言》获取,地址可以直接点击获取。

rust 猜数字游戏示例代码

  • 1 示例代码
  • 2 运行结果
    • 2.1 都输入数字的测试结果
    • 2.2 输入非数值的运行结果
  • 3 关键代码分析
    • 3.1 use std::io
    • 3.2 use rand::Rng
    • 3.3 std::cmp::Ordering
    • 3.4 let mut guess = String::new();
    • 3.5 io::stdin().read_line(&mut guess)
    • 3.6 let guess: u32 = match guess.trim().parse()
    • 3.7 match guess.cmp(&secret_number)
    • 3.8 loop

1 示例代码

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

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

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

    loop {
        println!("Please input your guess.");

        let mut guess = String::new();

        io::stdin()
            .read_line(&mut guess)
            .expect("Failed to read line");

        let guess: u32 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => {
            	println!("Please input digital num!");
            	continue
            },
        };

        println!("You guessed: {}", guess);

        match guess.cmp(&secret_number) {
            Ordering::Less => println!("Too small!"),
            Ordering::Greater => println!("Too big!"),
            Ordering::Equal => {
                println!("You win!");
                break;
            }
        }
    }
}

2 运行结果

想要看运行结果直接运行cargo run即可

2.1 都输入数字的测试结果

rlk@rlk:guess_num$ cargo run 
    Updating crates.io index
  Downloaded rand_core v0.6.4
  Downloaded rand v0.8.5
  Downloaded rand_chacha v0.3.1
  Downloaded ppv-lite86 v0.2.16
  Downloaded libc v0.2.136
  Downloaded getrandom v0.2.8
  Downloaded cfg-if v1.0.0
  Downloaded 7 crates (791.8 KB) in 1.12s
   Compiling libc v0.2.136
   Compiling cfg-if v1.0.0
   Compiling ppv-lite86 v0.2.16
   Compiling getrandom v0.2.8
   Compiling rand_core v0.6.4
   Compiling rand_chacha v0.3.1
   Compiling rand v0.8.5
   Compiling guess_num v0.1.0 (/home/rlk/work/learn_rust/guess_num)
    Finished dev [unoptimized + debuginfo] target(s) in 3m 43s
     Running `target/debug/guess_num`
Guess the number!
Please input your guess.
33
You guessed: 33
Too small!
Please input your guess.
50
You guessed: 50
Too big!
Please input your guess.
45
You guessed: 45
Too big!
Please input your guess.
40
You guessed: 40
Too small!
Please input your guess.
43
You guessed: 43
Too big!
Please input your guess.
41
You guessed: 41
You win!
rlk@rlk:guess_num$

2.2 输入非数值的运行结果

rlk@rlk:guess_num$ cargo run 
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/guess_num`
Guess the number!
Please input your guess.
33
You guessed: 33
Too small!
Please input your guess.
50
You guessed: 50
Too big!
Please input your guess.
q
Please input digital num!
Please input your guess.
39
You guessed: 39
Too small!
Please input your guess.
44
You guessed: 44
Too small!
Please input your guess.
47
You guessed: 47
Too small!
Please input your guess.
49
You guessed: 49
You win!
rlk@rlk:guess_num$

3 关键代码分析

3.1 use std::io

use std::io;	//引入 io 输入/输出库到当前作用域

3.2 use rand::Rng

use rand::Rng	//引入随机数生成器

3.3 std::cmp::Ordering

std::cmp::Ordering	//比较两个值时可能出现的三种结果

3.4 let mut guess = String::new();

let mut guess = String::new();	//定义可变字符串变量guess

3.5 io::stdin().read_line(&mut guess)

	io::stdin().read_line(&mut guess)
            .expect("Failed to read line");	//读取命令行输入内容,如果没有输入数据则提示读失败

3.6 let guess: u32 = match guess.trim().parse()

下面的代码是将读取的字符串变量先去除首尾的空行,然后再转换为u32类型的数值变量,同时判断转换之后的是否为数值类型,如果是这直接返回数值;否则,打印提示,然后继续。

let guess: u32 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => {
            	println!("Please input digital num!");
            	continue
            },
        };

3.7 match guess.cmp(&secret_number)

下面的代码是匹配字符是比要猜的数值是大还是小。

match guess.cmp(&secret_number) {
            Ordering::Less => println!("Too small!"),
            Ordering::Greater => println!("Too big!"),
            Ordering::Equal => {
                println!("You win!");
                break;
            }
        }

3.8 loop

loop 是一个循环,如果没有跳出的处理则会一直处于循环执行的状态。

loop {
	...
}

你可能感兴趣的:(编程语言-rust,rust,rust,猜数字游戏示例代码)