Baffled why my crate isn't found

I'm very new to rust, but not new to programming. I'm trying to learn the language with the Hands on Rust build a dungeon crawler book. I've just added a new file to a project that previously compiled and ran without issue, but now my new file isn't being found. I'm not seeing what I'm missing. I stripped all the game code out and I can still reproduce the issue.

$ find *
Cargo.toml
src
src/player.rs
src/map.rs
src/main.rs
$ cat Cargo.toml
[package]
name = "dungeon_what"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

$ cargo check
    Checking dungeon_what v0.1.0 ($HOME/src/hands-on-rust/dungeon_what)
error[E0432]: unresolved import `crate::player`
 --> src/main.rs:7:18
  |
7 |   pub use crate::player::*;
  |                  ^^^^^^ could not find `player` in the crate root

error[E0433]: failed to resolve: use of undeclared type `Player`
  --> src/main.rs:21:15
   |
21 |       player: Player::new( 2, 3),
   |               ^^^^^^ use of undeclared type `Player`

error[E0412]: cannot find type `Player` in this scope
  --> src/main.rs:14:11
   |
14 |   player: Player,
   |           ^^^^^^ not found in this scope

Some errors have detailed explanations: E0412, E0432, E0433.
For more information about an error, try `rustc --explain E0412`.
error: could not compile `dungeon_what` due to 3 previous errors

And my src files:

src/main.rs:

mod map;

mod prelude {
  pub const SCREEN_WIDTH: i32 = 80;  
  pub const SCREEN_HEIGHT: i32 = 50;  
  pub use crate::map::*;
  pub use crate::player::*;
}

use prelude::*;

struct State {
  map: Map,
  player: Player,
}

impl State {
  pub fn new() -> Self {
    Self { 
      map: Map::new(),
      player: Player::new( 2, 3),
    }
  } // new
  fn tick(&mut self) {
    self.map.render();
    self.player.render();
  } // tick
} // State


fn main() {
  let game : State = State::new();
  game.tick();
}

src/map.rs:

use crate::prelude::*;
const NUM_TILES: usize = (SCREEN_WIDTH * SCREEN_HEIGHT) as usize;

#[derive(Clone,Copy,PartialEq)]
pub enum TileType {
  Wall,
  Floor,
}

pub struct Map {
  pub tiles: Vec<TileType>,
}

impl Map {
  pub fn new() -> Self {
    Self {
      tiles: vec![TileType::Floor; NUM_TILES],
    }
  } // new

  pub fn render(&self) {
    println!("A Map appears");
  } // render
} // Map

And the not found src/player.rs:

use crate::prelude::*;

pub struct Player {
  pub position_x: i32,
  pub position_y: i32,
}

impl Player {
  pub fn new(position_x: i32, position_y: i32) -> Self {
    Self {
      position_x, position_y
    }
  } // new
 
  pub fn render(&self, ctx:&mut BTerm) {
    println!("A Player appears");
  }
} // Player

Doesn't the use crate::player::* import the player.rs file? The same syntax is seemingly working for map.rs in the same scope and same directory. The player.rs file is seemingly invisible to cargo check: note that it has a bit I forgot to edit out, that is not being flagged as an error (the BTerm bit in the render function).

You have to declare your modules. You have

mod map;

So add:

+mod player;
1 Like

Aha. Yes. That works. Why doesn't rustc --explain E0433 offer that hint? (Is there something else I should have turned on to get more verbose hints?)

Hmm, good question. I didn't find an existing issue about this, but you could file a diagnostic issue suggesting that addition. And/or to offer a more specific hint if an undeclared file with the corresponding name/filepath exists (though I'm not sure how feasible the latter is).

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.