rust-analyzer LSP
Rust language server integration providing comprehensive code intelligence through rust-analyzer.
Capabilities
- - Code intelligence: Autocomplete, go-to-definition, find references
- Error detection: Real-time diagnostics for compilation errors
- Refactoring: Rename symbols, extract function/variable
- Analysis: Macro expansion, type hints, inlay hints
- Supported extensions: INLINECODE0
Installation
Via rustup (recommended)
CODEBLOCK0
Via Homebrew (macOS)
CODEBLOCK1
Via package manager (Linux)
CODEBLOCK2
Manual download
Download pre-built binaries from the
releases page.
Verify installation:
CODEBLOCK3
Usage
The language server runs automatically in LSP-compatible editors. For manual operations:
Format code
CODEBLOCK4
Run linter
CODEBLOCK5
Build and test
CODEBLOCK6
Check without building
CODEBLOCK7
Configuration
Create .rust-analyzer.json in project root:
CODEBLOCK8
Integration Pattern
When editing Rust code:
- 1. rust-analyzer provides real-time diagnostics
- Run
cargo fmt to format code - Use
cargo clippy for linting - Run
cargo test before committing
Common Cargo Commands
- -
cargo new <name> - Create new project - INLINECODE6 - Compile project
- INLINECODE7 - Build and run
- INLINECODE8 - Run tests
- INLINECODE9 - Fast compile check
- INLINECODE10 - Run linter
- INLINECODE11 - Format code
- INLINECODE12 - Generate and open docs
More Information
rust-analyzer LSP
通过 rust-analyzer 提供全面代码智能的 Rust 语言服务器集成。
功能
- - 代码智能:自动补全、跳转到定义、查找引用
- 错误检测:编译错误的实时诊断
- 重构:重命名符号、提取函数/变量
- 分析:宏展开、类型提示、内联提示
- 支持的扩展名:.rs
安装
通过 rustup(推荐)
bash
rustup component add rust-analyzer
通过 Homebrew(macOS)
bash
brew install rust-analyzer
通过包管理器(Linux)
bash
Ubuntu/Debian
sudo apt install rust-analyzer
Arch Linux
sudo pacman -S rust-analyzer
手动下载
从
发布页面下载预构建二进制文件。
验证安装:
bash
rust-analyzer --version
使用
语言服务器在兼容 LSP 的编辑器中自动运行。手动操作:
格式化代码
bash
cargo fmt
运行 linter
bash
cargo clippy
构建和测试
bash
cargo build
cargo test
不构建进行检查
bash
cargo check
配置
在项目根目录创建 .rust-analyzer.json:
json
{
checkOnSave: {
command: clippy
},
inlayHints: {
typeHints: true,
parameterHints: true
}
}
集成模式
编辑 Rust 代码时:
- 1. rust-analyzer 提供实时诊断
- 运行 cargo fmt 格式化代码
- 使用 cargo clippy 进行 lint 检查
- 提交前运行 cargo test
常用 Cargo 命令
- - cargo new - 创建新项目
- cargo build - 编译项目
- cargo run - 构建并运行
- cargo test - 运行测试
- cargo check - 快速编译检查
- cargo clippy - 运行 linter
- cargo fmt - 格式化代码
- cargo doc --open - 生成并打开文档
更多信息