rust-lsp

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Rust Analyzer Agent Skill

Rust Analyzer Agent Skill

When to use

使用场景

MUST use the MCP tools when the task involves:
  • Resolving where a symbol is defined, including across crate boundaries
  • Finding all references to a function, type, field, or trait method
  • Inspecting type signatures, inferred types, or trait implementations
  • Searching for symbols by name across a workspace
  • Renaming a symbol across all usage sites
SHOULD prefer the MCP tools over grep or file reads when the task requires Rust-aware semantic understanding (scoping, imports, trait dispatch, macro expansion).
Fall back to grep or file reads when the task is not Rust-specific, targets string literals or comments, or covers files outside the Rust compilation.
必须在以下任务场景中使用MCP工具:
  • 解析符号的定义位置,包括跨crate边界的情况
  • 查找函数、类型、字段或trait方法的所有引用
  • 检查类型签名、推断类型或trait实现
  • 跨工作区按名称搜索符号
  • 在所有使用位置安全重命名符号
应优先在任务需要具备Rust感知能力的语义理解(作用域、导入、trait调度、宏展开)时,使用MCP工具而非grep或文件读取。
当任务不涉及Rust特定内容、针对字符串字面量或注释,或覆盖Rust编译范围外的文件时,退而使用grep或文件读取。

Installation

安装

If the Rust LSP tools are not available yet, run
scripts/install.sh
from this skill before using the MCP tools. Treat the tools as "not available yet" whenever the current session does not expose the Rust LSP MCP tool APIs: attempt the install/setup path first, then verify whether the tools become usable.
如果Rust LSP工具尚未可用,请在使用MCP工具前运行此Skill中的
scripts/install.sh
脚本。当当前会话未暴露Rust LSP MCP工具API时,即视为工具“尚未可用”:请先尝试安装/设置流程,然后验证工具是否可用。

Common workflows

常见工作流

Navigate to a definition

导航至定义

  1. definitions
    at the symbol position
  2. Read the target file at the returned location
  1. 在符号位置调用
    definitions
  2. 读取返回位置对应的目标文件

Find all callers

查找所有调用者

  1. references
    at the function name position
  2. Review the returned locations
  1. 在函数名称位置调用
    references
  2. 查看返回的位置列表

Inspect a type

检查类型

  1. hover
    at the position — returns the resolved type even for inferred types, trait objects, and generic instantiations
  1. 在目标位置调用
    hover
    ——即使是推断类型、trait对象和泛型实例,也会返回解析后的类型

Find a symbol by name

按名称查找符号

  1. workspace_symbols
    with the name as query
  2. Read the returned location if needed
  1. 使用名称作为查询参数调用
    workspace_symbols
  2. 如有需要,读取返回的位置

Rename a symbol safely

安全重命名符号

  1. open_document
    with the file contents
  2. rename_symbol
    at the position with the new name
  3. Apply the returned workspace edits to disk
  4. close_document
    when done
  1. 使用文件内容调用
    open_document
  2. 在目标位置调用
    rename_symbol
    并传入新名称
  3. 将返回的工作区编辑内容应用到磁盘
  4. 完成后调用
    close_document

Update in-memory content for analysis

更新内存中的分析内容

Position-based tools work on saved files without
open_document
. Use the document lifecycle only when analyzing unsaved or in-memory content:
  1. open_document
    to synchronize contents with rust-analyzer
  2. replace_document
    to update contents (version auto-increments)
  3. close_document
    when done (idempotent)
基于位置的工具无需调用
open_document
即可处理已保存的文件。仅当分析未保存或内存中的内容时,才需要使用文档生命周期流程:
  1. 调用
    open_document
    与rust-analyzer同步内容
  2. 调用
    replace_document
    更新内容(版本自动递增)
  3. 完成后调用
    close_document
    (操作具有幂等性)

React to project structure changes

响应项目结构变更

  1. reload_workspace
    after editing
    Cargo.toml
    or adding/removing crates
  2. rebuild_proc_macros
    if proc-macro source changed
  1. 在编辑
    Cargo.toml
    或添加/移除crates后调用
    reload_workspace
  2. 如果proc-macro源代码发生变更,调用
    rebuild_proc_macros