词表 · 第 8 篇
Rust
1. allocator
- 译:分配器 | Rust:控制堆内存分配/释放策略的类型(
GlobalAlloctrait) - 例
- Plug in a custom allocator for tracking allocations. — 接入自定义分配器以跟踪分配。
- The global allocator serves every
Box::new. — 全局分配器服务于每一次Box::new。
- 地道:swap out the global allocator(替换全局分配器)
- 短语:GlobalAlloc trait · custom allocator · jemalloc / mimalloc
2. async
- 译:异步 | Rust:由编译器把函数编译为状态机的零成本并发原语
- 例
async fnreturns aFuture. —async fn返回一个Future。- Async functions don’t run until awaited. — async 函数在被 await 之前不会执行。
- 地道:async/await(异步等待)
- 短语:async fn · async block · async runtime
3. await
- 译:等待 | Rust:暂停 future 直至其就绪,并把控制权交还执行器
- 例
.awaitpolls the future. —.await对 future 进行轮询。- Don’t block inside an
.await. — 别在.await内做阻塞操作。
- 地道:await a future(等待一个 future)
- 短语:.await · await point · suspend at await
4. borrow
- 译:借用 | Rust:通过引用访问值而不夺取所有权
- 例
- Borrow the value instead of moving it. — 借用值而不是转移它。
- You can have many shared borrows at once. — 同时可以存在多个共享借用。
- 地道:borrow, don’t clone(借用,不要克隆)
- 短语:borrow a value · shared borrow · mutable borrow
5. borrow checker
- 译:借用检查器 | Rust:编译期校验引用生命周期与别名规则的组件
- 例
- The borrow checker rejected my code. — 借用检查器拒绝了我的代码。
- Fight the borrow checker, then thank it. — 与借用检查器搏斗,然后感谢它。
- 地道:fighting the borrow checker(与借用检查器搏斗)
- 短语:borrow checker · aliasing rule · NLL(非词法生命周期)
6. boxing
- 译:装箱 | Rust:把值放到堆上(
Box<T>),获得稳定地址并让不定大小类型大小已知 - 例
- Box a recursive type to give it a known size. — 装箱一个递归类型让其大小已知。
- Boxing incurs a heap allocation. — 装箱会产生一次堆分配。
- 地道:box it up(把它装箱)
- 短语:Box
· box a value · boxed slice
7. cargo
- 译:货船;货运 | Rust:官方构建系统与包管理器
- 例
cargo buildcompiles your crate. —cargo build编译你的 crate。- Add a dependency in Cargo.toml. — 在 Cargo.toml 中添加依赖。
- 地道:let cargo handle it(交给 cargo 处理)
- 短语:cargo build · cargo run · Cargo.toml
8. crate
- 译:板条箱 | Rust:编译单元;可发布到 crates.io 的包
- 例
- A binary crate produces an executable. — 二进制 crate 生成可执行文件。
- Pull a crate from crates.io. — 从 crates.io 拉取一个 crate。
- 地道:publish a crate(发布一个 crate)
- 短语:binary crate · library crate · crates.io
9. derive
- 译:派生 | Rust:通过
#[derive]自动为类型实现 trait - 例
- Derive
Debugto print the type. — 派生Debug以打印该类型。 #[derive(Clone, Copy)]opts into cheap copies. —#[derive(Clone, Copy)]启用低成本拷贝。
- Derive
- 地道:just derive it(直接派生就好)
- 短语:#[derive] · derive macro · DeriveInput
10. drop
- 译:丢弃;释放 | Rust:值离开作用域时运行的析构钩子(
Droptrait) - 例
- Drop runs cleanup in reverse declaration order. — drop 按声明逆序执行清理。
- Call
std::mem::dropto release early. — 调用std::mem::drop提前释放。
- 地道:drop it like it’s hot(立刻释放)
- 短语:Drop trait · drop guard · early drop
11. dyn
- 译:dynamic(动态);
dyn关键字 | Rust:标记 trait 对象的动态分发 - 例
Box<dyn Error>is a trait object. —Box<dyn Error>是一个 trait 对象。- Use
dynfor runtime polymorphism. — 用dyn实现运行时多态。
- 地道:dyn dispatch(动态分发)
- 短语:dyn Trait · fat pointer · vtable
12. enum
- 译:枚举 | Rust:可承载变体与关联值的代数数据类型
- 例
Option<T>is a built-in enum. —Option<T>是一个内置 enum。- Enums can carry data per variant. — 枚举的每个变体都能携带数据。
- 地道:model with enum(用 enum 建模)
- 短语:enum variant · Option
· match on enum
13. feature flag
- 译:特性开关 | Rust:Cargo 中按
#[cfg(feature = "...")]条件编译的开关 - 例
- Gate optional deps behind a feature flag. — 用 feature flag 把可选依赖隔开。
--features "serde"enables the flag. —--features "serde"启用该 flag。
- 地道:behind a feature flag(隐藏在特性开关后)
- 短语:#[cfg(feature)] · cargo features · feature unification
14. foreign function interface
英 /ˈfɒrən ˈfʌŋkʃn ˈɪntəfeɪs/ · 美 /ˈfɔːrən ˈfʌŋkʃn ˈɪntərfeɪs/ · 🔊 英 美
- 译:外部函数接口(FFI) | Rust:与 C ABI 互操作的机制
- 例
- Call into C libraries via FFI. — 通过 FFI 调用 C 库。
extern "C"declares an FFI function. —extern "C"声明一个 FFI 函数。
- 地道:across the FFI boundary(跨越 FFI 边界)
- 短语:extern “C” · FFI boundary · bindgen
15. generic
- 译:泛型 | Rust:通过单态化在编译期生成具体类型的零成本抽象
- 短语:generic fn · type parameter · monomorphization
16. impl
- 译:implementation(实现);
impl关键字 | Rust:为类型实现方法或 trait 的代码块 - 例
impl Trait for Typedefines a trait impl. —impl Trait for Type定义 trait 实现。- Inherent
implblocks hold methods. — 固有impl块包含方法。
- 地道:inside the impl block(在 impl 块内)
- 短语:impl block · impl Trait · inherent impl
17. interior mutability
英 /ɪnˈtɪəriə(r) ˌmjuːtəˈbɪləti/ · 美 /ɪnˈtɪriər ˌmjuːtəˈbɪləti/ · 🔊 英 美
- 译:内部可变性 | Rust:通过
UnsafeCell让不可变引用背后能修改数据 - 例
RefCell<T>provides interior mutability. —RefCell<T>提供内部可变性。- Interior mutability moves the check to runtime. — 内部可变性把检查推迟到运行期。
- 地道:shared mutability(共享的可变性)
- 短语:RefCell
· UnsafeCell · Cell
18. iterator
- 译:迭代器 | Rust:实现
Iteratortrait 的惰性序列生成器 - 例
- Iterators are lazy by default. — 迭代器默认是惰性的。
- Chain iterators with
mapandfilter. — 用map和filter串联迭代器。
- 地道:lazy iterator(惰性迭代器)
- 短语:Iterator trait · map / filter · into_iter
19. lifetimes
- 译:生命周期 | Rust:编译期标注引用有效区域的泛型参数
- 例
- Annotate lifetimes when the compiler can’t infer them. — 当编译器无法推断时标注生命周期。
'staticlives for the whole program. —'static贯穿整个程序。
- 地道:lifetime elision(生命周期省略)
- 短语:lifetime parameter · ‘static · lifetime elision
20. macro
- 译:宏 | Rust:声明宏(
macro_rules!)与过程宏两套元编程系统 - 例
println!is a declarative macro. —println!是一个声明宏。- Procedural macros generate code at compile time. — 过程宏在编译期生成代码。
- 地道:write a macro(写一个宏)
- 短语:macro_rules! · proc macro · derive macro
21. match
- 译:匹配 | Rust:基于模式对值进行穷尽分支的表达式
- 例
- Match must be exhaustive. — match 必须穷尽所有情况。
- Match on
Resultto handle errors. — 对Result进行 match 以处理错误。
- 地道:match against(对……做匹配)
- 短语:match expression · match arm · wildcard _
22. monomorphization
- 译:单态化 | Rust:为泛型的每个具体类型实例生成一份专用代码的编译过程
- 例
- Monomorphization gives generics zero runtime cost. — 单态化让泛型没有运行时开销。
- The compiler monomorphizes
Vec<u8>andVec<i32>separately. — 编译器为Vec<u8>和Vec<i32>分别生成代码。
- 地道:compile-time specialization(编译期特化)
- 短语:static dispatch · code bloat · generic instantiation
23. move
- 译:转移所有权 | Rust:将值的所有权从一处交付给另一处
- 例
- Moving a value ends its old scope. — 转移值后原作用域不再可用。
- Closures capture by move when marked
move. — 标了move的闭包按移动捕获。
- 地道:move semantics(移动语义)
- 短语:move closure · move a value · Copy vs Move
24. mut
- 译:mutable(可变);
mut关键字 | Rust:声明可变绑定或可变引用 - 例
- Mark a binding
mutto reassign it. — 标记mut才能重新赋值。 &mut Tis an exclusive reference. —&mut T是独占引用。
- Mark a binding
- 地道:mut binding(可变绑定)
- 短语:let mut · &mut T · mut self
25. mutex
- 译:互斥锁 | Rust:线程间共享可变状态的同步原语(
Mutex<T>) - 例
- Wrap shared data in a Mutex. — 把共享数据包进 Mutex。
Mutex::lockblocks until acquired. —Mutex::lock阻塞直到获取锁。
- 地道:lock the mutex(锁住互斥锁)
- 短语:Mutex
· lock().unwrap() · poisoning
26. ownership
- 译:所有权 | Rust:每个值有唯一所有者,离开作用域即被释放
- 例
- Ownership is Rust’s headline feature. — 所有权是 Rust 的招牌特性。
- Transfer ownership by moving. — 通过转移来传递所有权。
- 地道:ownership model(所有权模型)
- 短语:ownership transfer · owner · single owner
27. panic
- 译:恐慌 | Rust:不可恢复错误的展开或中止机制
- 例
- Indexing out of bounds panics. — 越界索引会触发 panic。
unwrap()panics onErrorNone. —unwrap()遇到Err或None会 panic。
- 地道:don’t panic in a library(库里不要 panic)
- 短语:panic! macro · catch_unwind · abort vs unwind
28. pattern matching
- 译:模式匹配 | Rust:用模式解构并按形状分支的能力
- 例
- Pattern matching powers
match,if let, andlet else. — 模式匹配驱动match、if let和let else。 - Bind sub-values while matching. — 在匹配的同时绑定子值。
- Pattern matching powers
- 地道:destructure with patterns(用模式解构)
- 短语:match · if let · let-else
29. pin
- 译:钉住;
Pin类型 | Rust:保证自引用类型不会被再次移动的封装 - 例
Pin<Box<dyn Future>>is the classic combo. —Pin<Box<dyn Future>>是经典组合。- Pinning enables self-referential futures. — pinning 让自引用 future 成为可能。
- 地道:pin a future(钉住一个 future)
- 短语:Pin
· Unpin · pin_mut
30. pub
- 译:public(公开);
pub关键字 | Rust:声明对外可见的项 - 例
- Mark functions
pubto export them. — 给函数加pub来导出。 pub(crate)restricts visibility to the crate. —pub(crate)把可见性限制在 crate 内。
- Mark functions
- 地道:pub(crate)(crate 内可见)
- 短语:pub fn · pub(crate) · pub struct
31. reference
- 译:引用(Rust:借用的具现化
&T/&mut T) - 短语:&T · &mut T · shared reference
32. result
- 译:结果(Rust:
Result<T, E>可恢复错误的载体) - 短语:Result<T, E> · Ok(T) · Err(E)
33. rustfmt
- 译:Rust 格式化工具 | Rust:官方代码风格自动格式化器
- 例
- Run
cargo fmtbefore committing. — 提交前运行cargo fmt。 - rustfmt enforces a canonical style. — rustfmt 强制统一风格。
- Run
- 地道:let rustfmt handle it(交给 rustfmt 处理)
- 短语:cargo fmt · rustfmt.toml · format on save
34. send
- 译:发送 | Rust:标记类型可安全地在线程间转移所有权的
Sendtrait - 例
Rc<T>is notSend. —Rc<T>不是Send。- Only
Sendtypes can cross threads by value. — 只有Send类型能按值跨线程。
- 地道:Send vs Sync(可转移与可共享)
- 短语:Send trait · move to thread · thread-safe
35. sync
- 译:同步 | Rust:标记类型可安全地被多线程共享引用的
Synctrait - 例
Arc<T>isSyncwhenT: Send + Sync. —Arc<T>在T: Send + Sync时是Sync。Syncis about&Tbeing shareable across threads. —Sync关注&T能否跨线程共享。
- 地道:Sync trait(共享 trait)
- 短语:Sync trait · Arc
· shared reference
36. trait
- 译:特质;特征 | Rust:一组方法签名构成、可被类型实现的接口
- 例
- Implement
Displayto pretty-print. — 实现Display来美化输出。 - Traits can have default methods. — trait 可以带默认方法。
- Implement
- 地道:trait object(trait 对象)
- 短语:impl Trait · trait bound · dyn Trait
37. type inference
- 译:类型推断 | Rust:编译器根据上下文推导类型而无需显式标注
- 例
- Let type inference fill in the types. — 让类型推断补全类型。
let x = 42;infersi32by default. —let x = 42;默认推断为i32。
- 地道:type inference saves keystrokes(类型推断省键盘)
- 短语:let binding · turbofish · Hindley-Milner
38. unsafe
- 译:不安全 | Rust:解除编译器部分检查、由程序员担保不变式
- 例
- Wrap
unsafein a safe abstraction. — 用安全抽象包住unsafe。 - Dereferencing raw pointers requires
unsafe. — 解引用裸指针需要unsafe。
- Wrap
- 地道:scoping unsafe(限定 unsafe 范围)
- 短语:unsafe fn · unsafe block · raw pointer
39. workspace
- 译:工作区 | Rust:多个 crate 共享同一 Cargo.lock 与 target 目录的集合
- 例
- A workspace shares one Cargo.lock. — 一个 workspace 共用一个 Cargo.lock。
- Put related crates in one workspace. — 把相关 crate 放进同一 workspace。
- 地道:cargo workspace(cargo 工作区)
- 短语:Cargo workspace · workspace member · shared target