Rust can't find module, even though I followed the rust-lang book [Solved...for now]

I've been trying to restructure my code (the commit before the restructuring) into multiple files because my entire parser along with test code would just be unreasonably large for a single file. I tried to split up the files, but got error after error after error. After trying dozens of suggestions found on the internet I finally decided to start over from scratch and follow the The Rust Programming Language book's Crates and Modules chapter. I didn't have as many modules and they obviously had different names, but my structure looks like this:

.
|
|--Cargo.toml
|--Cargo.lock
|--src
     |--ast
     |    |--mod.rs
     |    |--structs.rs
     |
     |--lib.rs
     |--main.rs

Edit: The Cargo.toml is the default you get from typing cargo new tomllib so no need to reproduce it here.

// lib.rs
mod ast;

#[test]
fn it_works() {
}
// main.rs
extern crate tomllib;

fn main() {
    println!("Hello in English: {}", tomllib::ast::structs::hello());
}
// mod.rs
mod structs;
// structs.rs
fn hello() -> String {
    "Hello!".to_string()
}

Now when I compile this I should get an error about tomllib::ast::structs::hello() being private, but it doesn't even get that far because it can't find the ast module:

main.rs:4:38: 4:66 error: failed to resolve. Could not find `ast` in `tomllib` [E0433]
main.rs:4     println!("Hello in English: {}", tomllib::ast::structs::hello());
                                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

This is the exact type of error I was getting when I was restructuring my project into different files. It kept saying that it couldn't find any of the modules I had made even though I followed guides on how to make and use modules. Now I can't even get a simple example to work.

What's going on here?

Ugh, I figured it out. I changed mod ast in lib.rs to pub mod ast and it now gives the expected "function is private error". I'm still wary about recreating my whole project again from scratch though because not much should change.

Funny, I'm getting the 'same could not find `...` in `...`' error, even though everything is pub. The library itself builds fine but the project using it telling one of the modules is there and the other does not exist?

Open a new thread.

3 Likes