当前位置:网站首页>rust猜数字游戏
rust猜数字游戏
2022-07-01 05:26:00 【pilaf1990】
视频参考bilibili:https://www.bilibili.com/video/BV1hp4y1k7SV?p=6&vd_source=0299ee6ffe3815178fa7786dd32402d1
Cargo.toml文件(类似java的maven pom.xml文件,用于管理依赖版本的)内容:
[package]
name = "guessing_game"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rand = "0.8.5"
main.rs的文件内容:
use rand::Rng;
use std::cmp::Ordering;
use std::io;
fn main() {
println!("猜数字游戏开始");
// rand = "0.8.5" 版本的gen_range要传递low..high或low..=high这种方式
// 生成一个1到100的随机数
let secret_number = rand::thread_rng().gen_range(1..101);
loop {
println!("请猜测一个数字");
// mut 修饰变量表示变量的值可变
let mut guess = String::new();
// 从控制台读取一个数字
io::stdin().read_line(&mut guess).expect("无法读取");
let guess:u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => {
println!("输入的不是数字,重新输入");
continue;
},
};
println!("你猜测的数字是:{}",guess);
match guess.cmp(&secret_number) {
Ordering::Less => println!("你输入的太小了"),
Ordering::Greater => println!("你输入的太大了"),
Ordering::Equal => {
println!("你赢了,猜对了,神秘数字就是:{}",guess);
break;
}
}
}
}
程序运行效果:
边栏推荐
- [RootersCTF2019]babyWeb
- 提高企业产品交付效率系列(1)—— 企业应用一键安装和升级
- Memtable for leveldb source code analysis
- eBPF Cilium实战(2) - 底层网络可观测性
- Common solutions for mobile terminals
- What can the points mall Games bring to businesses? How to build a points mall?
- Actual combat: gateway api-2022.2.13
- Flowable source code comment (XXXIX) task listener
- Global and Chinese markets of gps/gnss receiver modules 2022-2028: Research Report on technology, participants, trends, market size and share
- JDBC常见面试题
猜你喜欢

busybox生成的东西

How to create a progress bar that changes color according to progress

LevelDB源码分析之LRU Cache

Speed regulation and stroke control based on Ti drv8424 driving stepper motor

Spanner 论文小结

Copier le matériel de conseils de bébé ne peut pas être vide, comment résoudre?

云原生存储解决方案Rook-Ceph与Rainbond结合的实践

提高企业产品交付效率系列(1)—— 企业应用一键安装和升级

基于TI DRV8424驱动步进电机实现调速和行程控制

Application and principle of ThreadPoolExecutor thread pool
随机推荐
JS random verification code
Set集合详细讲解
Simple implementation of database connection pool
Global and Chinese market of enterprise wireless LAN 2022-2028: Research Report on technology, participants, trends, market size and share
多表操作-外键级联操作
数据库连接池的简单实现
SSGSSRCSR区别
Unity 使用Sqlite
Rust基础入门之变量绑定与解构
Fiber Bragg grating (FBG) notes [1]: waveguide structure and Bragg wavelength derivation
Design and application of immutable classes
0xc000007b应用程序无法正常启动解决方案(亲测有效)
JDBC常见面试题
JDBC common interview questions
Copier le matériel de conseils de bébé ne peut pas être vide, comment résoudre?
Floweable source code annotation (40) class delegation
Web Security (x) what is OAuth 2.0?
In depth understanding of condition source code interpretation and analysis of concurrent programming
Application and principle of ThreadPoolExecutor thread pool
QT waiting box production