Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Essential Rust Tooling for C# Developers
C# 开发者需要掌握的 Rust 工具生态

What you’ll learn: Rust’s development tools mapped to their C# equivalents — Clippy (Roslyn analyzers), rustfmt (dotnet format), cargo doc (XML docs), cargo watch (dotnet watch), and VS Code extensions.
本章将学到什么: 把 Rust 开发工具映射到熟悉的 C# 对应物上,包括 Clippy(类似 Roslyn analyzers)、rustfmt(类似 dotnet format)、cargo doc(类似 XML 文档生成)、cargo watch(类似 dotnet watch),以及 VS Code 扩展。

Difficulty: 🟢 Beginner
难度: 🟢 入门

Tool Comparison
工具对照表

C# ToolRust EquivalentInstallPurpose
Roslyn analyzers
Roslyn 分析器
Clippy
Clippy
rustup component add clippy
rustup component add clippy
Lint + style suggestions
代码检查与风格建议
dotnet format
dotnet format
rustfmt
rustfmt
rustup component add rustfmt
rustup component add rustfmt
Auto-formatting
自动格式化
XML doc comments
XML 文档注释
cargo doc
cargo doc
Built-in
内置
Generate HTML docs
生成 HTML 文档
OmniSharp / Roslyn
OmniSharp / Roslyn
rust-analyzer
rust-analyzer
VS Code extension
VS Code 扩展
IDE support
IDE 支持
dotnet watch
dotnet watch
cargo-watch
cargo-watch
cargo install cargo-watch
cargo install cargo-watch
Auto-rebuild on save
保存后自动重建

cargo-expand
cargo-expand
cargo install cargo-expand
cargo install cargo-expand
See macro expansion
查看宏展开结果
dotnet audit
dotnet audit
cargo-audit
cargo-audit
cargo install cargo-audit
cargo install cargo-audit
Security vulnerability scan
扫描安全漏洞

Clippy: Your Automated Code Reviewer
Clippy:自动化代码审查员

# Run Clippy on your project
# 在项目上运行 Clippy
cargo clippy

# Treat warnings as errors (CI/CD)
# 把警告当错误处理,适合 CI/CD
cargo clippy -- -D warnings

# Auto-fix suggestions
# 自动修复可处理建议
cargo clippy --fix
#![allow(unused)]
fn main() {
// Clippy catches hundreds of anti-patterns:
// Clippy 能揪出成百上千种反模式

// Before Clippy:
// Clippy 提示前:
if x == true { }           // warning: equality check with bool
let _ = vec.len() == 0;    // warning: use .is_empty() instead
for i in 0..vec.len() { }  // warning: use .iter().enumerate()

// After Clippy suggestions:
// 按 Clippy 建议修改后:
if x { }
let _ = vec.is_empty();
for (i, item) in vec.iter().enumerate() { }
}

rustfmt: Consistent Formatting
rustfmt:统一格式

# Format all files
# 格式化所有文件
cargo fmt

# Check formatting without changing (CI/CD)
# 只检查格式,不改文件,适合 CI/CD
cargo fmt -- --check
# rustfmt.toml — customize formatting (like .editorconfig)
# rustfmt.toml:自定义格式规则,类似 .editorconfig
max_width = 100
tab_spaces = 4
use_field_init_shorthand = true

cargo doc: Documentation Generation
cargo doc:文档生成

# Generate and open docs (including dependencies)
# 生成并打开文档,连依赖文档一起带上
cargo doc --open

# Run documentation tests
# 运行文档测试
cargo test --doc
#![allow(unused)]
fn main() {
/// Calculate the area of a circle.
/// 计算圆的面积。
///
/// # Arguments
/// # 参数
/// * `radius` - The radius of the circle (must be non-negative)
/// * `radius` - 圆的半径,必须是非负数
///
/// # Examples
/// # 示例
/// ```
/// let area = my_crate::circle_area(5.0);
/// assert!((area - 78.54).abs() < 0.01);
/// ```
///
/// # Panics
/// # Panic 情况
/// Panics if `radius` is negative.
/// 如果 `radius` 为负数,就会 panic。
pub fn circle_area(radius: f64) -> f64 {
    assert!(radius >= 0.0, "radius must be non-negative");
    std::f64::consts::PI * radius * radius
}
// The code in /// ``` blocks is compiled and run during `cargo test`!
// `/// ``` ` 代码块里的示例会在 `cargo test` 时被编译并执行。
}

cargo watch: Auto-Rebuild
cargo watch:自动重建

# Rebuild on file changes (like dotnet watch)
# 文件变化时自动重建,类似 dotnet watch
cargo watch -x check          # Type-check only (fastest)
                              # 只做类型检查,速度最快
cargo watch -x test           # Run tests on save
                              # 保存时跑测试
cargo watch -x 'run -- args'  # Run program on save
                              # 保存时带参数运行程序
cargo watch -x clippy         # Lint on save
                              # 保存时顺手跑 Clippy

cargo expand: See What Macros Generate
cargo expand:看看宏到底生成了什么

# See the expanded output of derive macros
# 查看 derive 宏展开后的结果
cargo expand --lib            # Expand lib.rs
                              # 展开 lib.rs
cargo expand module_name      # Expand specific module
                              # 展开指定模块
ExtensionPurpose
rust-analyzer
rust-analyzer
Code completion, inline errors, refactoring
代码补全、行内报错、重构支持
CodeLLDB
CodeLLDB
Debugger (like Visual Studio debugger)
调试器,体验上类似 Visual Studio 调试器
Even Better TOML
Even Better TOML
Cargo.toml syntax highlighting
给 Cargo.toml 提供语法高亮
crates
crates
Show latest crate versions in Cargo.toml
在 Cargo.toml 里显示最新 crate 版本
Error Lens
Error Lens
Inline error/warning display
直接在行内显示错误和警告

For deeper exploration of advanced topics mentioned in this guide, see the companion training documents:
如果要继续深挖本章提到的高级主题,可以继续阅读下面这些配套训练材料:

  • Rust Patterns — Pin projections, custom allocators, arena patterns, lock-free data structures, and advanced unsafe patterns
    Rust Patterns:讲 Pin 投影、自定义分配器、arena 模式、无锁数据结构,以及更深入的 unsafe 模式。
  • Async Rust Training — Deep dive into tokio, async cancellation safety, stream processing, and production async architectures
    Async Rust Training:深入讲 tokio、异步取消安全、stream 处理和生产环境异步架构。
  • Rust Training for C++ Developers — Useful if your team also has C++ experience; covers move semantics mapping, RAII differences, and template vs generics
    Rust Training for C++ Developers:如果团队里也有 C++ 背景成员,这份材料会讲清 move 语义映射、RAII 差异,以及模板和泛型的关系。
  • Rust Training for C Developers — Relevant for interop scenarios; covers FFI patterns, embedded Rust debugging, and no_std programming
    Rust Training for C Developers:适合互操作场景,内容涵盖 FFI 模式、嵌入式 Rust 调试和 no_std 编程。