如何获取要显示的类型提示?

问题描述

我见过 youtubers 和这样的人在 VSC 中使用 rust-analyzer 插件在 Rust 上工作,他们会显示可选的类型注释,即使它不一定写在代码中。就像我在编辑器中输入 foo(a,b) 一样,它会自动显示 foo(a: A,b :B),其中 :A:B 呈淡灰色,可能甚至没有写在文件中,只是视觉提示?很好,我不知道这是 VSC 还是锈分析仪的功能?我的 rust-analyzer 有两个设置 Parameter Hints 和 TypeHints 都设置为启用。

解决方法

在这种情况下,您正在寻找 parameter hints。要显示提示的函数也需要有多个参数。

确保启用该设置:

设置(用户界面)

Inlay hints: Parameter hints

设置 (JSON)

"rust-analyzer.inlayHints.parameterHints": true

然后您应该得到类似于以下内容的结果:

fn add(x: u32,y: u32) -> u32 {
    x + y    
}

fn main() {
    let result = add(x: 4,y: 2);
}

确保只启用了 rust-analyzer,因为它可能与 rls 发生冲突。一个警告 was added,如果两者都启用,它会提到以下内容:

You have both rust-analyzer (matklad.rust-analyzer) and Rust (rust-lang.rust)
plugins enabled. These are known to conflict and cause various functions of
both plugins to not work correctly. You should disable one of them.
,

rust-analyzer 显示镶嵌提示:

  • 局部变量的类型
  • 函数参数的名称
  • 链式表达式的类型

您可以通过将其添加到您的 settings.json 来切换镶嵌提示:

{
  "rust-analyzer.inlayHints.enable": true
}

或者您可以在您的 VSCode 首选项中搜索“rust inlay”。