3.guessing_game 读取用户输入

This commit is contained in:
Shikong 2022-06-26 15:24:08 +08:00
parent 9c1278ee65
commit c9cb4d209e
2 changed files with 34 additions and 0 deletions

View 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]

View 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)
}