Essential Keywords Reference
核心关键字速查表
What you’ll learn: A compact keyword map for Java developers so Rust syntax stops looking alien during the first few chapters.
本章将学习: 给 Java 开发者准备的一份紧凑关键字映射表,让 Rust 语法在前几章里别再显得那么陌生。Difficulty: 🟢 Beginner
难度: 🟢 初级
This chapter is a quick reference, not a replacement for the conceptual chapters.
这一章是速查表,不是概念章节的替代品。
| Rust keyword | Rough Java analogy | What it usually means |
|---|---|---|
let | local variable declaration 局部变量声明 | bind a value to a name 把一个值绑定到名字上 |
mut | mutable local variable 可变局部变量 | allow reassignment or mutation 允许重绑定或修改 |
fn | method or function declaration 方法或函数声明 | define a function 定义函数 |
struct | class or record shell 类或 record 外壳 | define a data type with fields 定义带字段的数据类型 |
enum | enum plus sealed hierarchy 枚举加 sealed 层级 | tagged union with variants 带分支的代数数据类型 |
impl | method block 方法实现块 | attach methods or trait impls 挂方法或 trait 实现 |
trait | interface | shared behavior contract 行为契约 |
match | switch expression switch 表达式 | exhaustive pattern matching 穷尽模式匹配 |
if let | guarded destructuring 条件解构 | handle one successful pattern 处理一个匹配成功的分支 |
while let | loop while match succeeds 匹配成功时循环 | consume values until pattern stops matching 持续处理直到模式失配 |
pub | public visibility 公开可见性 | expose outside the module 向模块外暴露 |
crate | module or artifact root 模块或产物根 | current package boundary 当前包边界 |
use | import | bring names into scope 把名字引入当前作用域 |
mod | package or nested module 包或嵌套模块 | declare a module 声明模块 |
ref | bind by reference in a pattern 在模式里按引用绑定 | avoid moving during pattern matching 模式匹配时避免 move |
move | capture by value 按值捕获 | transfer ownership into closure or thread 把所有权带进闭包或线程 |
async | async method marker 异步方法标记 | function returns a future 函数返回 future |
await | future completion point future 完成点 | suspend until result is ready 挂起直到结果就绪 |
unsafe | dangerous low-level block 低层危险块 | programmer must uphold invariants 开发者自己维持约束 |
where | generic bounds clause 泛型约束子句 | move trait bounds out of angle brackets 把约束挪出尖括号 |
Self | current class type 当前类型 | current implementing type 当前实现类型 |
dyn | interface reference 接口引用 | dynamic dispatch through a trait object 通过 trait object 动态分发 |
const | compile-time constant 编译期常量 | inlined immutable value 被内联的不可变值 |
static | static field 静态字段 | process-wide storage 进程级存储 |
Three Keywords That Need Extra Attention
三个需要额外留神的关键字
mut
mut
Mutability is explicit on the binding:
可变性直接写在绑定上:
#![allow(unused)]
fn main() {
let x = 1;
let mut y = 2;
y += 1;
}
match
match
match is not just a switch statement. It is a pattern-matching expression and must usually cover every case.match 不只是 switch 语句,它是模式匹配表达式,而且通常要求覆盖所有情况。
move
move
Java developers often underestimate move. In Rust it matters whenever values enter closures, threads, or async tasks.
很多 Java 开发者会低估 move 的重要性。只要值要进入闭包、线程或异步任务,它就立刻变得关键。
Keep this table nearby during the first pass through the book. After a few chapters, most of these keywords become second nature.
前几章先把这张表放在手边。读上几章之后,大多数关键字就会自然顺下来了。