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

bin+lib guessing_utils

Various guessing utilities for working with guesses in (0..101) number range

3 stable releases

1.0.4 Jul 8, 2023

#13 in #guessing

MIT license

9KB
111 lines

Guessing utilities

This crate provides various guessing utilities for working with guesses in (0..101) number range. Check the documentation for more.

Fully functional example

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;
            }
        }
    }
}

Dependencies

~315KB