Rust笔记--002 基础入门(中)

学习参考:https://course.rs/basic/intro.html 1.1 模式匹配 match匹配 通用形式 match target { 模式1 => 表达式1, 模式2 => { 语句1; 语句2; 表达式2 }, _ => 表达式3 } 示例 enum Direction { East, West, North, South, } fn main() { let dire = Direction::South; match dire { Direction::East => println!("East"), Direction::North | Direction::South => { println!("South or North"); }, _ => (), }; } 或 fn main() { let dire = Direction::South; match dire { Direction::East => println!("East"), Direction::North | Direction::South => { println!("South or North"); }, other => (), }; } 注: ...

June 22, 2025 · 7 min · 3077 words · liuzifeng

Rust笔记--001 基础入门(上)

学习参考:https://course.rs/basic/intro.html 1.1 变量 变量绑定 let a = "hello world" rust最核心的原则:所有权 所有权,简单来讲,任何内存对象都是有主人的,绑定是把"hello world"绑定给一个变量a,让a成为"hello world"的主人 变量的可变性 rust变量在默认情况下是不可变的 通过mut关键字将变量变为可变的 let mut a = 5; a = 6; 创建一个变量却不适用,用下划线_,在模式匹配会用到 let _a = 5; 变量解构 从一个相对复杂的变量中,匹配出该变量的一部分 ...

June 7, 2025 · 21 min · 10288 words · liuzifeng