(I'm a Rust beginner) I have three files : main.rs, board.rs and case.rs. I want to include case.rs into board.rs, and board.rs into main.rs, so the board uses the case, and we can access the board in main.
I've successfully added the board into main, but the way I did doesn't seem to work for the second part.
I've tried to encapsulate every file's content into "mod {}" but it doesn't change the problem. Also I tried every combinations of "mod" and "use".
Every file is in the folder src/, and I'd like them not to move from there if possible.
main.rs
mod board;
fn main() {
let mut b: Board = Board::new();
}
board.rs
mod case;
pub struct Board {
board: [ Case; 9 ]
}
// There is also the impl part of course, let's keep it short
case.rs
pub enum Case { Empty, Full(Player) }
Using VSCode with the Rust plugin, the "case" word on the first line of the board.rs file is underlined red, and it says :
"src/case.rs
file not found for module case
help: name the file either board\case.rs or board\case\mod.rs inside the directory "src""
Why doesn't it search in the current directory?
Your files could look like as follows:
case.rs:
board.rs:
main.rs:
Basically you create a
crate
(a binary one, because of yourmain.rs
) and thatcrate
can have modules. A module can be a file, or it can be a folder as well (if it happens to have amod.rs
). (And for the sake of completeness, it could also be an inline module without direct relation to the file system.)Your
mod
statements (the ones which are indicating files and folders, not the ones which you use to create inline modules) should either be placed at the top level of yourcrate
(e.g. in yourmain.rs
orlib.rs
) or at the module levels (in yourmod.rs
files) depending on the desired structure.For further info on this, please read the The Rust Programming Language book's relevant chapter: Packages and Crates.