| name | rust-tools |
| description | Rust 工具链技能。提供 Rust 工具链的完整参考,包括 Cargo、Rustdoc、Rustup、Clippy、Rustfmt 等。TRIGGER when: 用户使用 Cargo 命令、生成文档、配置 Rust 工具链、运行 Clippy 检查、格式化代码、或需要了解 Rust 工具链时触发。SKIP: 纯 Rust 代码编写。 |
Rust 工具链技能
本技能提供 Rust 工具链的完整参考,包括 Cargo、Rustdoc、Rustup、Clippy、Rustfmt 等。
触发条件
当用户执行以下操作时触发此技能:
- 使用 Cargo 命令
- 生成 Rust 文档
- 配置 Rust 工具链
- 运行 Clippy 检查
- 格式化代码
跳过条件
以下情况不触发此技能:
- 纯 Rust 代码编写(使用
rust-language 技能)
Cargo
项目管理
cargo new my-project
cargo new my-lib --lib
cargo build
cargo build --release
cargo run
cargo run -- arg1 arg2
cargo check
cargo clean
依赖管理
[dependencies]
serde = "1.0"
serde_json = "1.0"
tokio = { version = "1", features = ["full"] }
[dev-dependencies]
assert_cmd = "2"
predicates = "3"
[build-dependencies]
cc = "1"
cargo add serde
cargo add serde --features derive
cargo add tokio --features full
cargo update
cargo tree
工作空间
[workspace]
members = [
"member1",
"member2",
]
[workspace.dependencies]
serde = "1.0"
特性
[features]
default = ["std"]
std = []
alloc = []
full = ["std", "alloc"]
cargo build --features full
cargo build --no-default-features --features alloc
Rustdoc
文档注释
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
文档属性
#![warn(missing_docs)]
#![deny(missing_docs)]
pub mod my_module {
}
生成文档
cargo doc
cargo doc --open
cargo doc --document-private-items
cargo doc --check
文档测试
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
Rustup
工具链管理
rustup toolchain list
rustup toolchain install stable
rustup toolchain install nightly
rustup toolchain install 1.70.0
rustup default stable
rustup update
rustup toolchain uninstall nightly
目标管理
rustup target list --installed
rustup target add thumbv7em-none-eabihf
rustup target add wasm32-unknown-unknown
rustup target remove thumbv7em-none-eabihf
组件管理
rustup component list --installed
rustup component add rust-src
rustup component add rust-analyzer
rustup component add clippy
rustup component add rustfmt
rustup component remove rust-src
Clippy
基本用法
cargo clippy
cargo clippy --fix
cargo clippy -- -W clippy::all
cargo clippy -- -W clippy::pedantic
配置 Clippy
msrv = "1.70"
avoid-breaking-exported-api = true
#![allow(clippy::too_many_arguments)]
#![deny(clippy::unwrap_used)]
#![warn(clippy::nursery)]
常见 lint
let x = &vec![1, 2, 3];
let x = vec![1, 2, 3];
fn foo(x: &str) -> &str {
x
}
let x = if condition { 1 } else { 2 };
Rustfmt
基本用法
cargo fmt
cargo fmt -- --check
rustfmt src/main.rs
配置 Rustfmt
edition = "2021"
max_width = 100
tab_spaces = 4
use_small_heuristics = "Default"
常见配置
edition = "2021"
max_width = 100
tab_spaces = 4
use_small_heuristics = "Default"
imports_granularity = "Crate"
group_imports = "StdExternalCrate"
测试
单元测试
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_add() {
assert_eq!(add(1, 2), 3);
}
#[test]
#[should_panic(expected = "divide by zero")]
fn test_divide_by_zero() {
divide(1, 0);
}
#[test]
fn test_with_result() -> Result<(), String> {
let result = add(1, 2);
if result == 3 {
Ok(())
} else {
Err(String::from("错误"))
}
}
}
集成测试
use my_lib::add;
#[test]
fn test_add_integration() {
assert_eq!(add(1, 2), 3);
}
测试命令
cargo test
cargo test test_add
cargo test tests::
cargo test -- --test-threads=4
cargo test -- --nocapture
基准测试
使用 criterion
use criterion::{black_box, criterion_group, criterion_main, Criterion};
fn fibonacci(n: u64) -> u64 {
match n {
0 => 1,
1 => 1,
n => fibonacci(n - 1) + fibonacci(n - 2),
}
}
fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("fib 20", |b| b.iter(|| fibonacci(black_box(20))));
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
运行基准测试
cargo bench
构建脚本
build.rs
fn main() {
println!("cargo:rustc-env=VERSION=1.0.0");
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-env-changed=VERSION");
cc::Build::new()
.file("src/helper.c")
.compile("helper");
}
最佳实践
- 使用 Cargo workspace:管理多个相关项目
- 配置 Clippy:保持代码质量
- 配置 Rustfmt:保持代码风格一致
- 编写文档:使用
cargo doc 生成文档
- 编写测试:单元测试和集成测试
- 使用基准测试:优化性能
常见陷阱
- 依赖版本冲突:使用
cargo tree 检查
- 构建缓存:使用
cargo clean 清理
- 特性冲突:注意特性依赖关系
- 测试并行:确保测试独立
- 文档链接:使用
cargo doc --check 检查