我在 VSCode 中有 rust-analyzer 扩展。
在设置中,我只Rust-analyzer › Check On Save: Command
从更改check
为clippy
(这应该无关紧要)。
问题是我在输入时没有看到错误,只有在我保存之后。
输入(没有错误):
已保存(可以看到错误):
可以改变吗?
sub_dir/Cargo.toml
(Cargo.toml 不在存储库根目录中)rust-analyzer.linkedProjects
所以我需要根据每个环境我想避免通过机器进行配置。你知道有什么解决方法吗?
我目前正在制作一个嵌入式系统。为此,我有 2 个不同的恐慌处理程序,具体取决于它是正常运行还是作为测试运行。
#[cfg(test)]
对于测试恐慌处理程序和
#[cfg(not(test))]
正常恐慌处理程序。
rust-analyzer 说:由于 #[cfg] 指令,代码处于非活动状态:测试已启用
并使该功能变灰。测试从未明确设置,因此我不能只是更改它,而且我不想禁用工作区中灰显的非活动代码。
有没有办法禁用 rust-analyzer 检查测试 cfg,或者仅针对此功能禁用灰色
我尝试查找有关测试标志的信息,但找不到任何信息,如果它很重要,我正在使用 VS Code
https://www.rust-lang.org/tools/install指出可以使用 WSL 安装 Rust,效果很好。然而, VSCode 的 rust-analyzer 插件默认只能利用主机系统的 rust 安装,因为它在启动过程中找不到货物安装,并失败并显示以下消息:
[ERROR rust_analyzer::main_loop] FetchWorkspaceError:
rust-analyzer failed to load workspace: "cargo" "--version" failed: program not found
Run Code Online (Sandbox Code Playgroud)
有没有办法配置 rust-analyzer 以使用我的 WSL Rust 安装?
当然,另一种选择是使用开发容器,但这对于我正在从事的特定项目来说会非常麻烦。
尽管构建项目时没有错误或警告,rust-analyzer
但 VS Code出现此错误。cargo build
rustc
版本:1.54.0
我想保留“稳定”rustc
版本,而不是切换到“夜间”版本。
在我的 main.rs 中,我可以很好地完成代码。但我没有在我的模块文件中得到它。
我的文件夹结构如下所示:
src/
|___game_components/
| |___card.rs
|___game_components.rs
|___main.rs
Run Code Online (Sandbox Code Playgroud)
该程序构建并运行得很好(除了一些未使用的警告)。str
当编辑我的 main.rs 文件时,我得到了,rand
和我的结构的代码完成Card
。然而,当编辑我的任一 card.rs 时,我根本没有得到任何代码完成,甚至对于该文件中定义的 Card 结构也是如此。
我尝试重新安装 rust-analyzer 并运行rustup update
,但没有运气。
我错过了什么,或者某个地方有错误吗?
编辑:添加文件内容
主要.rs:
pub mod game_components;
use game_components::card::Card;
fn main() {
println!("{:?}", Card::new(5));
}
Run Code Online (Sandbox Code Playgroud)
游戏组件.rs:
pub mod card;
Run Code Online (Sandbox Code Playgroud)
卡.rs:
const FACES: [&str; 13] = [
"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace",
];
const SUITS: [&str; 4] = ["Hearts", "Clubs", "Diamonds", "Spades"];
#[derive(Debug)]
pub struct …
Run Code Online (Sandbox Code Playgroud) 当我在 VSCode 中输入以下内容时:
let mut guess = String::new();
Run Code Online (Sandbox Code Playgroud)
我看到它变成了:
let mut guess: String = String::new();
Run Code Online (Sandbox Code Playgroud)
为什么会发生这种情况?
我将这两个问题合二为一,因为它们可能是相关的。几天前,我开始在 main 函数之前的 [#actix_rt::main] 行中出现此错误:
proc macro `main` not expanded: cannot find proc-macro server in sysroot `C:\Users\zerok\.rustup\toolchains\stable-x86_64-pc-windows-gnu`
Run Code Online (Sandbox Code Playgroud)
与此同时,在 VSCode 中,我的 rust-analyzer 扩展开始失败。我卸载了它,重新启动 VSCode,然后重新安装。它不断地给出同样的错误:
Failed to spawn one or more proc-macro servers.
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
fn foo(ok: bool) -> Result<i32, i32> {
if ok { Ok(0) } else { Err(0) }
}
fn main() {
let Ok(x) | Err(x) = foo(true); // rust-analyzer error: top-level or-patterns are not allowed in `let` bindings
if let Ok(x) | Err(x) = foo(true) { // rust-analyzer warn: irrefutable `if let` pattern
println!("Working!");
}
}
Run Code Online (Sandbox Code Playgroud)
或者这是一个 Rust 分析器错误?我尝试用谷歌搜索但找不到任何东西。
在 VSCode 中处理 Tauri 项目时,我从rust-analyzer插件收到以下错误消息。
rust-analyzer failed to discover workspace
Run Code Online (Sandbox Code Playgroud)
我知道问题的原因是我在 VSCode 中加载了 Tauri 项目作为我的工作区,该项目的根目录中没有 a Cargo.toml
,而是在 Rust 相关的子目录中src-tauri
。有没有办法告诉插件它应该引用这个src-tauri
子目录Cargo.toml
?src-tauri
解决方法是每次接触 Rust 代码时都要切换工作区,这很不方便。
包含所有内容的目录称为p
,位于p
:
.git
.gitignore
Cargo.lock
Cargo.toml
todo.md
src
target
Run Code Online (Sandbox Code Playgroud)
是src
:
action_functions.rs
execute_action_functions.rs
lib.rs
main.rs
network.r
Run Code Online (Sandbox Code Playgroud)
在network.rs
:
pub mod network{
use rand::Rng //to generate random numbers
//use crate::action_functions::{/*insert all the action functions */};
use rand_distr::{Normal, Distribution}; //to generate different dist. of random numbers
use serde::{Serialize, Deserialize}; //to save/load my neural network
use std::fs::File;
use std::io::{BufReader, BufWriter};
use chrono::Utc; //for timestamp
use std::path::Path; //for help in representing file paths
Run Code Online (Sandbox Code Playgroud)
然后我有一堆 pub 结构,然后我有impl NeuralNetwork { …
这是我的项目结构:
\n.\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 src\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 main.rs\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 sub_folder\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 mod.rs\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 sub_mod.rs\n
Run Code Online (Sandbox Code Playgroud)\n在 中sub_mod.rs
,如果我像这样导入,则货物不会警告我sub_folder/
:
.\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 src\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 main.rs\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 sub_folder\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 mod.rs\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 sub_mod.rs\n
Run Code Online (Sandbox Code Playgroud)\n但我做不到
\n#[path = "./sub_folder/mod.rs"]\nmod sub_folder;\n
Run Code Online (Sandbox Code Playgroud)\n但它main.rs
有效!
有没有更温和的方式sub_mod.rs
导入sub_folder/
?