4

In my main.rs I get code completion just fine. But I don't get it in my module files.

My folder structure looks like this:

src/
|___game_components/
|   |___card.rs
|___game_components.rs
|___main.rs

The program builds and runs just fine (aside from some unused warnings). And when editing my main.rs file I get code completion for str, rand and my Card struct. However when editing my either card.rs I don't get any code completion at all, not even for the Card struct that is defined in that file.

I have tried reinstalling rust-analyzer and I have ran rustup update, but no luck.

Am I missing something, or is there a bug somewhere?

Edit: added file contents

main.rs:

pub mod game_components;

use game_components::card::Card;

fn main() {
    println!("{:?}", Card::new(5));
}

game_components.rs:

pub mod card;

card.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]
        )
    }
}
YBStolker
  • 41
  • 1
  • 3

2 Answers2

4

The cargo.Toml points to the lib.rs and main.rs files , so the rust analyzer will only work with those files. To enable the rust analyzer in the other files such as card.rs and game_component.rs, you need to link them to the main.rs file.

game_component.rs should be:

pub mod card;

also ensure card.rs is in a folder named game_component. While main.rs should be:

 mod game_components;

use game_components::card::Card;

fn main() {
    println!("{:?}", Card::new(5));
}

Make sure card.rs is visible outide of it. in summary, linking the file to main.rs will make it work.that is how i solved mine when i encounter the issues.

thormighti
  • 96
  • 6
0

I faced this issue (Vscode + Rust-analyzer) on a project on a specific file (db.rs). autocompletion/jump to definition would not work on db.rs. There are no errors/warnings from rust-analyzer to understand why this is not working.

Old structure

  / model
    +-> db.rs
    +-> mod.rs

For now I moved db.rs to its own module and things are working. Will update this in future if there is a better way to resolve this.

New structure

  / db
     +-> mod.rs (old db.rs)
  / model
     +-> mod.rs