3.guessing_game 读取用户输入
This commit is contained in:
parent
9c1278ee65
commit
c9cb4d209e
8
3.guessing_game/Cargo.toml
Normal file
8
3.guessing_game/Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
[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]
|
26
3.guessing_game/src/main.rs
Normal file
26
3.guessing_game/src/main.rs
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
// 默认 导入 prelude
|
||||||
|
|
||||||
|
// 使用 库
|
||||||
|
use std::io;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!("猜数游戏");
|
||||||
|
|
||||||
|
// 声明变量
|
||||||
|
// let foo = 1;
|
||||||
|
// let bar = foo; // immutable
|
||||||
|
|
||||||
|
// rust 中 所有变量默认为不可变
|
||||||
|
// foo = 2; // 抛错
|
||||||
|
|
||||||
|
// 声明变量时 添加 mut 关键字 为可变变量
|
||||||
|
let mut guess = String::new(); // 创建空字符串 实例
|
||||||
|
|
||||||
|
io::stdin()
|
||||||
|
.read_line(&mut guess)
|
||||||
|
// read_line 返回 io::Result Ok, Err
|
||||||
|
// 捕获异常
|
||||||
|
.expect("读取失败");
|
||||||
|
|
||||||
|
println!("猜测的值为 {}",guess)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user