#numbers #utilities #guessing #range #guess #println #order

bin+lib guessing_utils

用于在 (0..101) 数字范围内处理猜测的多种工具

3 个稳定版本

1.0.4 2023年7月8日

#4 in #ordering

MIT 许可证

9KB
111

猜测工具

此包提供用于在 (0..101) 数字范围内处理猜测的各种工具。更多信息请参阅文档

完全功能示例

use guessing_utils;
use guessing_utils::Guess;

use std::io;
use std::cmp::Ordering;

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

    let secret_number = guessing_utils::gen_random();

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

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

        let guess: Guess = match guessing_utils::Guess::parse(&guess) {
            Ok(val) => val,
            Err(_) => continue,
        };

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

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

依赖项

~315KB