YBS*_*ker 4 code-completion rust visual-studio-code rust-analyzer
在我的 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 Card {
value: u8,
face: u8,
suit: u8,
}
impl Card {
pub fn new(value: u8) -> Card {
if value >= 52 {
panic!("Value cannot be larger than 51, got {}", value)
}
Card {
value,
face: value % 13,
suit: value / 13,
}
}
pub fn get_name(&self) -> String {
format!(
"{} of {}",
FACES[self.face as usize], SUITS[self.suit as usize]
)
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
Cargo.Toml 指向 lib.rs 和 main.rs 文件,因此 rust 分析器仅适用于这些文件。要在其他文件(例如 card.rs 和 game_component.rs)中启用 rust 分析器,您需要将它们链接到 main.rs 文件。
game_component.rs 应该是:
pub mod card;
Run Code Online (Sandbox Code Playgroud)
还要确保 card.rs 位于名为 game_component 的文件夹中。而 main.rs 应该是:
mod game_components;
use game_components::card::Card;
fn main() {
println!("{:?}", Card::new(5));
}
Run Code Online (Sandbox Code Playgroud)
确保 card.rs 在其外部可见。总之,将文件链接到 main.rs 将使其工作。这就是我遇到问题时解决问题的方法。