← 返回
内容创作
中文
Rust
Write idiomatic Rust avoiding ownership pitfalls, lifetime confusion, and common borrow checker battles.
编写符合 Rust 惯用风格的代码,避免所有权陷阱、生命周期困惑以及常见的借用检查冲突。
ivangdavila
内容创作
clawhub
v1.0.1 1 版本 99963.3 Key: 无需
#latest
概述
Quick Reference
| Topic | File | Key Trap |
|---|
| ------- | ------ | ---------- |
| Ownership & Borrowing | ownership-borrowing.md | Move semantics catch everyone |
| Strings & Types | types-strings.md | String vs &str, UTF-8 indexing |
| Errors & Iteration | errors-iteration.md | unwrap() in production, lazy iterators |
| Concurrency & Memory | concurrency-memory.md | Rc not Send, RefCell panics |
| Advanced Traps | advanced-traps.md | unsafe, macros, FFI, performance |
Critical Traps (High-Frequency Failures)
Ownership — #1 Source of Compiler Errors
- Variable moved after use — clone explicitly or borrow with
& for item in vec moves vec — use &vec or .iter() to borrowString moved into function — pass &str for read-only access
Borrowing — The Borrow Checker Always Wins
- Can't have
&mut and & simultaneously — restructure or interior mutability - Returning reference to local fails — return owned value instead
- Mutable borrow through
&mut self blocks all access — split struct or RefCell
Lifetimes — When Compiler Can't Infer
'static means CAN live forever, not DOES — String is 'static capable- Struct with reference needs
<'a> — struct Foo<'a> { bar: &'a str } - Function returning ref must tie to input —
fn get<'a>(s: &'a str) -> &'a str
Strings — UTF-8 Surprises
s[0] doesn't compile — use .chars().nth(0) or .bytes().len() returns bytes, not chars — use .chars().count()s1 + &s2 moves s1 — use format!("{}{}", s1, s2) to keep both
Error Handling — Production Code
unwrap() panics — use ? or match in production? needs Result/Option return type — main needs -> Result<()>expect("context") > unwrap() — shows why it panicked
Iterators — Lazy Evaluation
.iter() borrows, .into_iter() moves — choose carefully.collect() needs type — collect::>() or typed binding- Iterators are lazy — nothing runs until consumed
Concurrency — Thread Safety
Rc is NOT Send — use Arc for threadsMutex lock returns guard — auto-unlocks on drop, don't hold across awaitRwLock deadlock — reader upgrading to writer blocks forever
Memory — Smart Pointers
RefCell panics at runtime — if borrow rules violatedBox for recursive types — compiler needs known size- Avoid
Rc> spaghetti — rethink ownership
Common Compiler Errors (NEW)
| Error | Cause | Fix |
|---|
| ------- | ------- | ----- |
value moved here | Used after move | Clone or borrow |
cannot borrow as mutable | Already borrowed | Restructure or RefCell |
missing lifetime specifier | Ambiguous reference | Add <'a> |
the trait bound X is not satisfied | Missing impl | Check trait bounds |
type annotations needed | Can't infer | Turbofish or explicit type |
cannot move out of borrowed content | Deref moves | Clone or pattern match |
Cargo Traps (NEW)
cargo update updates Cargo.lock, not Cargo.toml — manual version bump needed- Features are additive — can't disable a feature a dependency enables
[dev-dependencies] not in release binary — but in tests/examplescargo build --release much faster — debug builds are slow intentionally
版本历史
共 1 个版本
-
v1.0.1
当前
2026-03-28 19:37 安全 安全
安全检测
腾讯云安全 (Sanbu)
安全,无风险
查看报告
🔗 相关推荐
ai-intelligence
ivangdavila
自我反思+自我批评+自我学习+自组织记忆。智能体评估自身工作、发现错误并持续改进。
★ 1,362
📥 318,686
content-creation
fly0pants
广告情报与应用数据分析助手,支持搜索广告素材、分析应用排名、下载量、收入及市场洞察,用于广告素材和竞品分析。
★ 295
📥 136,530
content-creation
biostartechnology
消除AI写作痕迹,使文本更自然真实。基于维基百科"AI写作特征"指南,识别并修正夸张象征、宣传用语、肤浅-ing分析、模糊归因、破折号滥用、三项排比、AI词汇、负面平行结构及冗长连接词等模式。
★ 860
📥 200,066