rust方法集

 

随机数、数字对比、控制台输入

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

fn main() {
    println!("please input a number(1-10):");

    let mut ss = String::new();
    io::stdin().read_line(&mut ss).expect("please input a value");

    println!("you input:{}",ss);
    let snum = rand::thread_rng().gen_range(1,10);
    println!("rand number:{}",snum);

    let ss: u32 = ss.trim().parse().ok().expect("please input a number");

    match ss.cmp(&snum) {
        Ordering::Less => println!("too smalll"),
        Ordering::Greater => println!("too big"),
        Ordering::Equal => println!("you win!"),
    }


}

 

Cargo.toml

[package]
name = "a1_guess"
version = "0.1.0"
authors = ["tanpengfei3 <tanpengfei3@jd.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rand = "0.7.3"

 

posted @ 2020-03-16 13:00  方诚  阅读(249)  评论(0编辑  收藏  举报