我正在阅读 Rust 书 [0] 的第 12.3 章。 特别是清单 12-14 中的这段代码:
use std::env;
use std::process;
use minigrep;
use minigrep::Config;
fn main() {
// --snip--
if let Err(e) = minigrep::run(config) {
// --snip--
}
}
但是,VS Code 扩展给我带来了一些错误:
在挖掘这个问题时,我发现在 2018 版之前,Rust 编译器会发现这些问题 [1]。 在将 Rust 更新到 1.32.0 并将edition = "2018"
到Cargo.toml
,我的代码在终端中运行得很好。 然而,通过 Rust 扩展,这些错误仍然存在于 VS Code 中。 是我还是扩展程序永远不会修复此错误?
[0] https://doc.rust-lang.org/book/ch12-03-improving-error-handling-and-modularity.html
[1] https://github.com/rust-lang/book/issues/1676#issuecomment -445876415
@tuvtran ,我在 Rust
我正在运行 Rust 1.33.0 和 RLS,但仍然遇到这些问题 - 一切构建和运行良好,但无法在编辑器中解决从板条箱中提取的库。
添加extern crate foo
修复了 VS Code 中的问题,所以也许工具还没有完全赶上? edition
设置为 2018 年。
你能把你的rust.*
VSCode 配置和 Cargo.toml 的内容贴出来吗? 假设edition = "2018"
在 Cargo.toml 中,最新的稳定 RLS 应该可以工作。
@tuvtran ,将edition = "2018"
更改为另一个(随机)年份,然后回到 2018 年在 VS Code 中为我解决了这个问题。
@tuvtran我认为你只需要改变你的 VSCode 主题,当你改变一切时应该会好得多。
我遇到了运行 rustc 1.33.0 的开篇文章中描述的问题。 我的大部分工具,包括 Rust 本身和 Rust VSCode 插件,都不是一周前安装的。 该项目是用 cargo new 新创建的,并且有edition = "2018"
。
将
edition = "2018"
更改为另一个(随机)年份,然后回到 2018 年在 VS Code 中为我解决了这个问题。
这也为我“修复”了它。
编辑:
更正 - 它只删除了错误,但它不会自动完成minigrep::
和里面的任何内容。 我添加了extern crate minigrep;
,现在一切都如我所愿:没有错误和自动完成。
@Xanewok
货物.toml:
[package]
name = "minigrep"
version = "0.1.0"
authors = ["Tobias Wehrum <[email protected]>"]
edition = "2018"
[dependencies]
我在哪里可以找到您想要的“ rust.*
VSCode 配置”? (我使用的是 Windows 7。)
@TobiasWehrum按 Ctrl + ,然后单击右侧的“{}”按钮打开 settings.json,您应该在那里更改了一些选项。
@Xanewok这些是我的整个 settings.json:
{
"[markdown]": {
},
"editor.formatOnType": true,
"editor.formatOnSave": true,
"terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe",
"explorer.openEditors.visible": 0,
"editor.suggestSelection": "first",
"vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue"
}
所以我已经预先创建了一个简单的cargo new t
和一个裸src/lib.rs
和src/main.rs
并且当我添加时
use t;
use t::MyStruct; // pub struct MyStruct { ... } inside the src/lib.rs
这按预期工作。
您能否提供非常详细的分步复制? 您是否从cargo new --bin project
,单独添加lib.rs
然后添加导入指令?
cargo new t
不会为我创建src/lib.rs
,只有src/main.rs
。
cargo new t
src/lib.rs
。pub struct MyStruct {}
到lib.rs
。src/main.rs
。use t::
,不要被智能感知提供MyStruct
,再次删除该行。extern crate t;
。MyStruct
。我在 Windows 10 上遇到了同样的问题。我看到删除该行并重新键入 use 行的行为相同。 我也有上述行为,更改 Cargo.toml 中的年份并将其改回。
退出 vscode 并重新打开,修复了问题。
我在运行rustc 1.36.0 (a53f9df32 2019-07-03)
时遇到了完全相同的问题。 添加extern crate
对我来说似乎没有解决任何问题,因为 RLS 根本找不到板条箱。
这些是我收到的错误和警告,尽管在我编译和运行程序时没有出现这些错误和警告:
我不得不关注@fzxu并重新启动 vscode(以及扩展 RLS)
@tuvtran ,将
edition = "2018"
更改为另一个(随机)年份,然后回到 2018 年在 VS Code 中为我解决了这个问题。
我正在使用带有 Rust(rls) 插件 v0.6.1 的 rust v1.35.0 和 VSCode 1.36.1。 这个技巧也对我有用。
退出 vscode 并重新打开,修复了问题。
按<F1>
并输入Rust: Restart the RLS
为我解决了这个问题。
我尝试了所有建议的解决方案,linting 错误消失了,但是当我添加extern crate minigrep;
时自动完成功能不起作用。 与@TobiasWehrum之前评论的相同
我也在这里尝试了所有方法,但仍然遇到此问题。
顺便说一句,我使用拱门
最近刚遇到这个问题,一些建议有效(将版本更改为其他版本,然后重新工作;也重新启动 RLS 工作)。 可能是RLS错误吗?
我用use crate::<my-mod>
修复它
同样的事情发生在我身上,每晚 rls 和 rust 1.40。 重新启动 VS 代码修复它。
在 VS Code 中,我通过运行“重新加载窗口”命令解决了这个问题。
Ctrl + shift + p
然后输入“重新加载窗口”
不幸的是,以前的解决方案都没有帮助。
基本上cargo run
工作得很好并运行该项目。 但是 VSCode 无法弄清楚 crate 在哪里:
代码:
use rand::Rng;
fn main() {
let rnd_num = rand::thread_rng().gen_range(1, 100);
println!("Random number: {}", rnd_num);
}
cargo run
来自终端:
[jurisl<strong i="19">@JurisLinuxPC</strong> game]$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/game`
Random number: 54
从代码编辑器运行时出错:
error[E0432]: unresolved import `rand`
--> main.rs:1:5
|
1 | use rand::Rng;
| ^^^^ maybe a missing crate `rand`?
error[E0433]: failed to resolve: use of undeclared type or module `rand`
--> main.rs:4:19
|
4 | let rnd_num = rand::thread_rng().gen_range(1, 100);
| ^^^^ use of undeclared type or module `rand`
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0432, E0433.
For more information about an error, try `rustc --explain E0432`.
添加extern crate rand;
导致相同的错误:
error[E0463]: can't find crate for `rand`
--> main.rs:1:1
|
1 | extern crate rand;
| ^^^^^^^^^^^^^^^^^^ can't find crate
error: aborting due to previous error
For more information about this error, try `rustc --explain E0463`.
更改edition = "2018"
- 没有帮助。
重新启动 RLS/VSCode - 没有帮助。
笔记:
基本上,在具有调试功能的两个编辑器(VSCode 和 Atom)中,与Intellij Rust
,它们都不适用于我,而且我无法在 Rust 中编写代码,因为工具实际上已损坏/不够成熟用于生产性工作。 如果我错了,请告诉我,这只是一个小标志的情况,每个人都忘记提及,在某些配置中丢失了。
执行重新加载窗口操作后,我不再在编辑器中看到误报错误,但我没有从 main.rs 获得 lib.rs 中符号的符号完成(我也在关注本书并执行 minigrep 项目.) 不过,我似乎确实从 lib.rs 文件中获得了符号补全。
@RebelCoderRU我遇到了和你完全一样的错误。
你的箱子编译了吗? 当板条箱中存在编译错误(例如缺少导入)时,自动完成中断时我遇到了同样的问题
我有同样的“未解决的导入”问题,但自动完成工作正常,
也看到了错误,重新加载窗口为我解决了问题
FWIW,我在 Emacs 中看到与 RLS 相同的问题,所以它看起来像是语言服务器问题,而不是 VSCode 问题。 重新启动服务器对我有用。
作为记录,我在此加载中遇到了同样的问题(代码编译正常,RLS 找不到依赖项):
VIM - Vi IMproved 8.2 (2019 Dec 12, compiled May 31 2020 11:42:24)
Included patches: 1-856
ALE (修订版 2d9380d75c5c27a3241925d24ab3be8977a43207)通过vimrc
使用以下配置...
" ...stuff assumed to be irrelevant...
let g:ale_lint_on_text_changed = 'never'
let g:ale_lint_on_insert_leave = 0
(即仅在保存时 lint),以及通过ftplugin/rust.vim
的以下配置:
let b:ale_linters = ['cargo', 'rls']
let b:ale_fixers = ['remove_trailing_lines', 'trim_whitespace']
let g:ale_rust_cargo_use_clippy = executable('cargo-clippy')
let g:ale_rust_rls_config = {
\ 'rust': {
\ 'clippy_preference': executable('cargo-clippy') ? 'on' : 'off'
\ }
\ }
(在#[rustfmt::skip]
没有像error: attributes are not yet allowed on ``if`` expressions
这样的错误之前,我不会自动运行 rustfmt )
rls 1.41.0 (5fde462 2020-02-21)
(这是rustup update stable
给我的匹配rustc 1.43.1 (8d69840ab 2020-05-04)
上次我运行它)
(换句话说,它看起来肯定是 RLS 中的问题,而不是 VSCode 扩展。)
但是,我可能会提示如何重现问题以及问题的来源。
我目前没有时间通过另一个编译来验证它是否会重现它(我在 Athlon II X2 270 上并且没有设置全局编译依赖项缓存并且测试涉及到 actix 的约 170 个依赖项),但是这就是我触发它的原因:
cargo new whatever
cargo add actix
(注意我在这里分心的错误)cargo run
actix-web
和actix-rt
,修复Cargo.toml
cargo run
Cargo.toml
,无论您如何摆弄:w
和:ALEToggle
而不诉诸killall rls
。...所以添加依赖项的类似舞蹈可能能够在 VSCode 下重现它,我怀疑这是 RLS 中的某种缓存失效错误。
问题仍然存在(因为我遇到了同样的情况 https://github.com/rust-lang/vscode-rust/issues/513#issue-414302862)
而像重新打开 VS Code 这样的操作可以神奇地解决这个问题 🤣 也许 rust 是真正的魔法
@SeanChao是的,这里仍然有问题,重新启动
我也收到此错误,但上述建议的解决方案均未解决我的问题。
api/mod.rs显然在那里。 我在我的 Windows 机器上也遇到了同样的问题。 它实际上是唯一不起作用的文件,所有其他文件都可以正常工作。
我也是..
退出 vscode 并重新打开,修复了问题。
🤣当人们遇到电脑问题时,我经常会翻白眼告诉他们“您尝试过重新启动吗?”,通常问题就消失了。 但是当轮到我自己时,我永远不会记得我自己的建议。
这个错误仍然可以重现,设置:VS Code (1.54.3), rustc 1.49.0 (e1884a8e3 2020-12-29), wsl 2 (Windows, but .. wsl 2, so ubuntu)。
@alessioalex 也同意使用 WSL2,这个问题很烦人,哈哈
最有用的评论
@tuvtran ,将
edition = "2018"
更改为另一个(随机)年份,然后回到 2018 年在 VS Code 中为我解决了这个问题。