Rust

Zed 原生支持 Rust 语言。

内联提示

可通过以下配置调整 Rust 中 rust-analyzer 的内联提示设置:

{
  "lsp": {
    "rust-analyzer": {
      "initialization_options": {
        "inlayHints": {
          "maxLength": null,
          "lifetimeElisionHints": {
            "enable": "skip_trivial",
            "useParameterNames": true
          },
          "closureReturnTypeHints": {
            "enable": "always"
          }
        }
      }
    }
  }
}

更多信息请参阅 Rust Analyzer 手册中的内联提示章节。

目标目录

可以通过initialization_options设置rust-analyzer目标目录:

{
  "lsp": {
    "rust-analyzer": {
      "initialization_options": {
        "rust": {
          "analyzerTargetDir": true
        }
      }
    }
  }
}

true设置为target/rust-analyzer会将目标目录指定为target/rust-analyzer。您也可以使用类似"target/analyzer"的字符串来自定义目录路径,而非使用true

二进制文件

您可以配置 Zed 应使用哪个 rust-analyzer 二进制文件。

默认情况下,Zed 会尝试在您的 $PATH 中查找 rust-analyzer 并使用它。如果该二进制文件成功执行 rust-analyzer --help,就会被采用。否则,Zed 将回退到安装自带的稳定版 rust-analyzer 并使用该版本。

若希望安装预发布版 rust-analyzer,您可以通过在 settings.json 中将 pre_release 设为 true 来指示 Zed:

{
  "lsp": {
    "rust-analyzer": {
      "fetch": {
        "pre_release": true
      }
    }
  }
}

若要禁用 Zed 查找 rust-analyzer 二进制文件的功能,可在 settings.json 中将 ignore_system_version 设为 true

{
  "lsp": {
    "rust-analyzer": {
      "binary": {
        "ignore_system_version": true
      }
    }
  }
}

如需使用自定义路径中的二进制文件,您可以指定 path 及可选的 arguments

{
  "lsp": {
    "rust-analyzer": {
      "binary": {
        "path": "/Users/example/bin/rust-analyzer",
        "arguments": []
      }
    }
  }
}

"path"必须是绝对路径。

备用目标平台

若希望rust-analyzer为当前平台之外的目标平台提供诊断信息(例如在macOS系统上为Windows平台进行诊断),可使用以下Zed语言服务器配置:

{
  "lsp": {
    "rust-analyzer": {
      "initialization_options": {
        "cargo": {
          "target": "x86_64-pc-windows-msvc"
        }
      }
    }
  }
}

若您正在使用rustup,可通过运行以下命令查看可用的目标平台三元组列表(aarch64-apple-darwinx86_64-unknown-linux-gnu等):

rustup target list --installed

语言服务器任务

Zed默认使用tree-sitter提供任务功能,但rust-analyzer额外提供了通过语言服务器协议查询文件相关任务的扩展方法。 该功能默认启用,可通过以下方式配置:

"lsp": {
  "rust-analyzer": {
    "enable_lsp_tasks": true,
  }
}

手动获取 Cargo 诊断信息

默认情况下,rust-analyzer 启用了 checkOnSave: true 功能,这会导致每次缓冲区保存都会触发 cargo check --workspace --all-targets 命令。 如果通过 checkOnSave: false 禁用此功能(参见上方服务器配置 JSON 示例),仍然可以通过在 Rust 文件中使用 editor: run/clear/cancel flycheck 命令来手动获取 cargo 诊断信息;当启用相关设置时,项目诊断编辑器也会通过 editor: run flycheck 命令刷新 cargo 诊断信息。

更多服务器配置

Rust-analyzer 手册 详细介绍了 rust-analyzer 语言服务器的各种功能和配置选项。 Zed 中的 Rust-analyzer 使用默认参数运行。

大型项目与性能表现

在大型项目中可能导致资源过度消耗的一个主要注意事项,是以下功能特性的组合使用:

rust-analyzer.checkOnSave (default: true)
    Run the check command for diagnostics on save.
rust-analyzer.check.workspace (default: true)
    Whether --workspace should be passed to cargo check. If false, -p <package> will be passed instead.
rust-analyzer.cargo.allTargets (default: true)
    Pass --all-targets to cargo invocation

这意味着每次 Zed 执行保存操作时,都会运行 cargo check --workspace --all-targets 命令,对完整项目(工作区)、lib、doc、test、bin、bench 及其他构建目标进行全面检查。

虽然这在小型项目中运行良好,但该方案不具备良好的可扩展性。

替代方案是使用任务,因为Zed已经提供了cargo check --workspace --all-targets任务功能,并且支持通过cmd/ctrl点击终端输出来定位错误,同时可以限制或完全关闭保存时检查功能。

保存时检查功能负责基于cargo check输出来返回部分诊断信息,关闭该功能将限制rust-analyzer仅使用其自带的诊断功能

建议参考手册中更多rust-analyzer.cargo.rust-analyzer.check.rust-analyzer.diagnostics.设置进行更精细的配置。 以下是Zed settings.json的配置片段(编辑并保存lsp.rust-analyzer部分后,语言服务器将自动重启):

{
  "lsp": {
    "rust-analyzer": {
      "initialization_options": {
        // get more cargo-less diagnostics from rust-analyzer,
        // which might include false-positives (those can be turned off by their names)
        "diagnostics": {
          "experimental": {
            "enable": true
          }
        },
        // To disable the checking entirely
        // (ignores all cargo and check settings below)
        "checkOnSave": false,
        // To check the `lib` target only.
        "cargo": {
          "allTargets": false
        },
        // Use `-p` instead of `--workspace` for cargo check
        "check": {
          "workspace": false
        }
      }
    }
  }
}

多项目工作区

如果你希望 rust-analyzer 在同一文件夹中分析多个未在 Cargo 工作区的 [members] 中列出的 Rust 项目, 可以在本地项目设置的 linkedProjects 中列出它们:

{
  "lsp": {
    "rust-analyzer": {
      "initialization_options": {
        "linkedProjects": ["./path/to/a/Cargo.toml", "./path/to/b/Cargo.toml"]
      }
    }
  }
}

代码片段

有一种方法可以从 rust-analyzer 获取自定义补全项,这些补全项会根据代码片段内容来转换代码:

{
  "lsp": {
    "rust-analyzer": {
      "initialization_options": {
        "completion": {
          "snippets": {
            "custom": {
              "Arc::new": {
                "postfix": "arc",
                "body": ["Arc::new(${receiver})"],
                "requires": "std::sync::Arc",
                "scope": "expr"
              },
              "Some": {
                "postfix": "some",
                "body": ["Some(${receiver})"],
                "scope": "expr"
              },
              "Ok": {
                "postfix": "ok",
                "body": ["Ok(${receiver})"],
                "scope": "expr"
              },
              "Rc::new": {
                "postfix": "rc",
                "body": ["Rc::new(${receiver})"],
                "requires": "std::rc::Rc",
                "scope": "expr"
              },
              "Box::pin": {
                "postfix": "boxpin",
                "body": ["Box::pin(${receiver})"],
                "requires": "std::boxed::Box",
                "scope": "expr"
              },
              "vec!": {
                "postfix": "vec",
                "body": ["vec![${receiver}]"],
                "description": "vec![]",
                "scope": "expr"
              }
            }
          }
        }
      }
    }
  }
}

调试

Zed 默认支持通过 CodeLLDBGDB 调试 Rust 二进制文件和测试。运行 debugger: start (f4|f4) 即可启动这些预配置的调试任务。

如需更精细的控制,您可以在 .zed/debug.json 中添加调试配置。请参考以下示例:

构建二进制文件后调试

[
  {
    "label": "Build & Debug native binary",
    "build": {
      "command": "cargo",
      "args": ["build"]
    },
    "program": "$ZED_WORKTREE_ROOT/target/debug/binary",
    // sourceLanguages is required for CodeLLDB (not GDB) when using Rust
    "sourceLanguages": ["rust"],
    "request": "launch",
    "adapter": "CodeLLDB"
  }
]

根据构建命令自动定位调试目标

当您使用cargo buildcargo test作为构建命令时,Zed 能够自动推断输出二进制文件的路径。

[
  {
    "label": "Build & Debug native binary",
    "adapter": "CodeLLDB",
    "build": {
      "command": "cargo",
      "args": ["build"]
    },
    // sourceLanguages is required for CodeLLDB (not GDB) when using Rust
    "sourceLanguages": ["rust"]
  }
]