返回顶部
🇺🇸 English
🇨🇳 简体中文
🇨🇳 繁體中文
🇺🇸 English
🇯🇵 日本語
🇰🇷 한국어
🇫🇷 Français
🇩🇪 Deutsch
🇪🇸 Español
🇷🇺 Русский
R

Rust

Write idiomatic Rust avoiding ownership pitfalls, lifetime confusion, and common borrow checker battles.

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 1.0.1
安全检测
已通过
1,863
下载量
4
收藏
概述
安装方式
版本历史

Rust

## 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 borrow - **`String` 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::<Vec<_>>()` or typed binding - **Iterators are lazy** — nothing runs until consumed ### Concurrency — Thread Safety - **`Rc` is NOT `Send`** — use `Arc` for threads - **`Mutex` lock returns guard** — auto-unlocks on drop, don't hold across await - **`RwLock` deadlock** — reader upgrading to writer blocks forever ### Memory — Smart Pointers - **`RefCell` panics at runtime** — if borrow rules violated - **`Box` for recursive types** — compiler needs known size - **Avoid `Rc<RefCell<T>>` 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/examples - **`cargo build --release` much faster** — debug builds are slow intentionally

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 rust-1776329485 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 rust-1776329485 技能

通过命令行安装

skillhub install rust-1776329485

下载 Zip 包

⬇ 下载 Rust v1.0.1

文件大小: 7.58 KB | 发布时间: 2026-4-17 14:42

v1.0.1 最新 2026-4-17 14:42
Initial release

Archiver·手机版·闲社网·闲社论坛·羊毛社区· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部