我开始学习 Rust 语言,但我遇到了rust-analyzer
vscode 扩展的问题。
这张图片没有为我提供有关问题根源的任何附加信息。我尝试查找与我的错误相关的文章,但失败了。请帮忙!
UPD1。
目录截图:
Cargo.toml内容截图:
guessing_game 目录是使用cargo new guessing_game
命令创建的
我在 Visual Studio Code 上使用 rust-analyzer 版本 0.2.408。
我正在编写一个命令行应用程序,它涉及在终端中居中文本。这是我编写的函数:
use console::{Alignment, pad_str};
fn get_padded_row(row: &str, width: u16, symbol: Option<char>) -> String {
let symbol = symbol.unwrap_or(' ');
return pad_str(row, width as usize, Alignment::Center, None)
.to_string()
.replace(' ', &symbol.to_string());
}
Run Code Online (Sandbox Code Playgroud)
这个函数工作得很好,没有错误。然后我写了一个测试:
#[cfg(test)]
mod tests {
use crate::get_padded_row;
#[test]
fn row_padding_dashes() {
let padded_row = get_padded_row("hello", 15, Some('-'));
assert_eq!(
padded_row, "-----hello-----".to_string(),
"`get_padded_row` was not correct, got `{}`", padded_row
);
}
}
Run Code Online (Sandbox Code Playgroud)
该代码仍然可以正常工作。无论是工作cargo run
还是cargo test
工作,该函数都通过了测试,并且cargo check
没有返回任何问题。但防锈分析给出了一个错误,从强调一切tr}; …
我想使用Rust和Actix-Web运行一个 Web 服务器。按照文档中的这些步骤操作后,一切都按预期工作:服务器在端口 8080 上运行。
我遇到的问题是 VSCode 显示错误
将错误悬停在 IDE 中时,工具提示中会显示此错误消息
proc 宏
main
未扩展:无法为 /Users/name/Documents/projects/project-rust/target/debug/deps/libactix_web_codegen-b262af45f63000f3.dylib 创建扩展器:Io(Custom { kind: InvalidData, error: DlOpen { desc: "dlopen (/Users/name/Documents/projects/project-rust/target/debug/deps/libactix_web_codegen-b262af45f63000f3.dylib,0x000A):尝试:'/Users/name/Documents/projects/project-rust/target/debug/deps /libactix_web_codegen-b262af45f63000f3.dylib'(mach-o 文件,但是不兼容的架构(有'arm64',需要'x86_64')),'/usr/local/lib/libactix_web_codegen-b262af45f63000f3.dylib'(没有这样的文件) , '/usr/lib/libactix_web_codegen-b262af45f63000f3.dylib' (没有这样的文件)" } })
环境 - macOS 蒙特利 12.1 (M1)
我见过 youtube 等人使用 rust-analyzer 插件在 VSC 中使用 Rust,他们会显示可选的类型注释,即使它不一定写在代码中。就像我foo(a,b)
在编辑器中输入内容一样,它会自动显示和为淡灰色的foo(a: A, b: B)
位置,可能甚至没有写在文件中,只是视觉提示?很好,但我不知道这是 VSC 还是 rust-analyzer 的功能吗?我的 rust-analyzer 有两个设置 Parameter Hints 和 TypeHints 均设置为启用。:A
:B
我在使用 VSCode 的调试器执行 Rust 项目时遇到错误。它在操作系统更新之前有效,但现在我遇到了问题。我的 Mac 是 2019 年款 Macbook Pro,配备 x86 芯片。
为了说明问题所在,我创建了一个简单的 Rust 项目,其中仅包含 hello world。
它只编译文件,但是当我点击Debug
上面的内容时main
,我通过调试控制台得到了这个
Console is in 'commands' mode, prefix expressions with '?'.
Launching: /Users/my_account/Desktop/test/target/debug/test
Launched process 5903
Process exited with code -1.
Run Code Online (Sandbox Code Playgroud)
另外,我注意到,当我第一次打开 VSCode 时,rust 分析器是这种奇怪的颜色,并且它状态为Failed to spawn one or more proc-macro servers
,但是当我点击重新加载工作区时,它会返回到编辑器的原始蓝色。
错误
好的
当我查看 VSCode 中的“输出”选项卡时,它没有说明 Rust 分析器的任何内容,因此我假设这意味着 Rust 分析器方面一切正常?
我尝试过的:
rustup
任何帮助是极大的赞赏!:)
在 IntelliJ IDEA 中,当我按 Ctrl+Shift+Alt+N 时,我可以搜索并转到项目中可访问的所有代码中的任何符号。例如,在 Java Maven 项目中,Go To Symbol 将搜索 JDK 标准库中的所有符号、项目的所有 Maven 依赖项以及项目源代码。有没有办法在 VSCode 中搜索/列出/转到类似的符号?
更多背景信息:这是一个通过rust-analyzer
扩展实现的 Rust 项目。用例是,我隐约记得某个地方有一个带有特定单词的方法,但我不知道在哪里(stdlib?依赖板条箱?我的代码?)。我想输入该单词并获取与其匹配的所有符号的列表。
我正在进行 rusttles 练习,并注意到 rust-analyzer 已打开,但没有生成任何内联错误消息或类型提示。基本上什么都不做。
我认为这是因为我没有打开其中有 Cargo.toml 的顶级 rusdlings 目录,所以我关闭了 VSCode,然后在 VSCode 中打开该目录作为我的工作区,并确保工作区设置为“受信任” 。尽管如此,锈迹分析仪仍然没有任何结果。
相比之下,我使用创建了自己的货物包cargo new
,然后在 VSCode 中打开它,并且 rust-analyzer 工作正常,有错误和类型提示等等。
为什么 rust-analyzer 不适用于 rusdlings 工作区?
VSCode 1.74.3 Debian 11 Rustlings 于 2023 年 1 月克隆
我已按照https://rust-analyzer.github.io/manual.html#installation上的说明安装了 rust-analyzer :
\n\xe2\x9c\x94 ~> rustup component add rust-src\ninfo: component \'rust-src\' is up to date\n
Run Code Online (Sandbox Code Playgroud)\n但是,我得到:
\n\xe2\x9c\x94 ~> rust-analyzer -v\nerror: \'rust-analyzer\' is not installed for the toolchain \'stable-x86_64-unknown-linux-gnu\'\n
Run Code Online (Sandbox Code Playgroud)\n我也尝试安装这个特定的工具链:
\n\xe2\x9c\x98-1 ~> rustup component add rust-src --toolchain stable-x86_64-unknown-linux-gnu\ninfo: component \'rust-src\' is up to date\n
Run Code Online (Sandbox Code Playgroud)\n但我仍然遇到同样的错误。
\n我在互联网上查找了错误,并在 https://github.com/rust-lang/rust-analyzer/issues/14776找到了示例讨论。但是,检查可执行位置会返回
\n\xe2\x9c\x94 ~> which rust-analyzer\n/home/jrmet/.cargo/bin/rust-analyzer\n
Run Code Online (Sandbox Code Playgroud)\n这完全在我的路径之内。另外,我可以这样做:
\n\xe2\x9c\x94 ~> /home/jrmet/.cargo/bin/rust-analyzer\nerror: \'rust-analyzer\' is not installed for the toolchain \'stable-x86_64-unknown-linux-gnu\'\n
Run Code Online (Sandbox Code Playgroud)\n我还一直在与其他目标合作:
\n\xe2\x9c\x94 ~> rustup …
Run Code Online (Sandbox Code Playgroud) 我有这样的代码:
#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
Run Code Online (Sandbox Code Playgroud)
...我想深入了解Debug
.
在 IntelliJ IDEA 中,我将光标放在上面Debug
,然后按cmd + b
导航到 Rust 源。
在 VSCode 中,我试图获得类似于 IDEA 中的行为:
Debug
)Go to definition (F12)
或Go to Implementations(cmd + F12)
VSCode 向我显示消息No definition found for 'Debug'
。
有没有办法像在IDEA中那样在VSCode中按源设置导航?
我在 VSCode 中使用这个 Rust 扩展。