首页  

rust猜数字游戏     所属分类 rust 浏览量 97
src/main.rs
Cargo.toml


main.rs use rand::Rng; use std::cmp::Ordering; use std::io; fn main() { let secret_number = rand::thread_rng().gen_range(1..101); loop { print!("please input a int number 1~100 :"); 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(_) => continue, }; match guess.cmp(&secret_number) { Ordering::Less => println!("Too small!"), Ordering::Greater => println!("Too big!"), Ordering::Equal => { println!("You win!"); break; } } } }
Cargo.toml [package] name = "guess_game" version = "0.1.0" edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] rand = "0.8.4"
[root@VM_0_16_centos guess_game]# cargo run Finished dev [unoptimized + debuginfo] target(s) in 0.01s Running `target/debug/guess_game` 50 please input a int number 1~100 :please input a int number 1~100 :Too small! 70 please input a int number 1~100 :Too big! 60 please input a int number 1~100 :Too big! 55 please input a int number 1~100 :Too big! 53 please input a int number 1~100 :Too big! 52 please input a int number 1~100 :Too big! 51 please input a int number 1~100 :You win!
完整代码 https://gitee.com/dyyx/hellocode/tree/master/web/tech/rust/guess_game

上一篇     下一篇
Go by Example

Python3 in 1 Hour

C By Example

REST接口测试工具 WizTools.org RESTClient

rust cargo

C程序设计语言笔记_01_入门