我已经看到 youtubers 和这样的人在 VSC 中使用 rust-analyzer 插件在 Rust 上工作,他们可以在其中显示可选的类型注释,即使它不一定写在代码中。就像我foo(a,b)
在编辑器中输入,它会自动显示和淡灰色foo(a: A, b :B)
的位置,甚至可能没有写在文件中,只是视觉提示?很好,我不知道这是 VSC 还是 rust-analyzer 的功能?我的 rust-analyzer 有两个设置 Parameter Hints 和 TypeHints 都设置为启用。:A
:B
2962 次
2 回答
5
您正在寻找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
. 添加了一个警告,如果两者都启用,则会提到以下内容:
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.
于 2021-02-12T15:46:18.967 回答
1
rust-analyzer 显示嵌体提示:
- 局部变量的类型
- 函数参数的名称
- 链式表达式的类型
您可以通过将其添加到您的settings.json
:
{
"rust-analyzer.inlayHints.enable": true
}
或者您可以在 VSCode 首选项中搜索“rust inlay”。
于 2021-02-12T15:55:50.913 回答