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 分析器)、 rustfmt(类似 dotnet format)、cargo doc(类似 XML 文档生成)、cargo watch(类似 dotnet watch),以及常见 VS Code 扩展。

Difficulty: Beginner

难度: 初级

Tool Comparison | 工具对照

C# ToolRust EquivalentInstallPurpose
Roslyn analyzersClippyrustup component add clippyLint + style suggestions
Roslyn 分析器Clippyrustup component add clippyLint 与代码风格建议
dotnet formatrustfmtrustup component add rustfmtAuto-formatting
dotnet formatrustfmtrustup component add rustfmt自动格式化
XML doc commentscargo docBuilt-inGenerate HTML docs
XML 文档注释cargo doc内置生成 HTML 文档
OmniSharp / Roslynrust-analyzerVS Code extensionIDE support
OmniSharp / Roslynrust-analyzerVS Code 扩展IDE 支持
dotnet watchcargo-watchcargo install cargo-watchAuto-rebuild on save
dotnet watchcargo-watchcargo install cargo-watch保存后自动重建
-cargo-expandcargo install cargo-expandSee macro expansion
-cargo-expandcargo install cargo-expand查看宏展开结果
dotnet auditcargo-auditcargo install cargo-auditSecurity vulnerability scan
dotnet auditcargo-auditcargo install cargo-audit安全漏洞扫描

Clippy: Your Automated Code Reviewer | Clippy:你的自动代码审查员

# Run Clippy on your project
cargo clippy

# Treat warnings as errors (CI/CD)
cargo clippy -- -D warnings

# Auto-fix suggestions
cargo clippy --fix
#![allow(unused)]
fn main() {
// Clippy catches hundreds of anti-patterns:

// Before 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:
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)
cargo fmt -- --check
# rustfmt.toml - customize formatting (like .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)
///
/// # Examples
/// ```
/// let area = my_crate::circle_area(5.0);
/// assert!((area - 78.54).abs() < 0.01);
/// ```
///
/// # Panics
/// Panics if `radius` is negative.
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 watch: Auto-Rebuild | cargo watch:自动重建

# Rebuild on file changes (like 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

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

# See the expanded output of derive macros
cargo expand --lib            # Expand lib.rs
cargo expand module_name      # Expand specific module
ExtensionPurpose
rust-analyzerCode completion, inline errors, refactoring
rust-analyzer代码补全、行内报错、重构支持
CodeLLDBDebugger (like Visual Studio debugger)
CodeLLDB调试器(类似 Visual Studio 调试体验)
Even Better TOMLCargo.toml syntax highlighting
Even Better TOML为 Cargo.toml 提供更好的高亮与编辑体验
cratesShow latest crate versions in Cargo.toml
crates显示 Cargo.toml 中 crate 的最新版本
Error LensInline error/warning display
Error Lens直接在代码行内显示错误与警告

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 projection、自定义分配器、arena 模式、无锁数据结构和高级 unsafe 模式
  • Async Rust Training - Deep dive into tokio, async cancellation safety, stream processing, and production async architectures
  • Async Rust Training - 深入 tokio、异步取消安全、流处理与生产级 async 架构
  • 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 编程