Trouble importing files into lib.rs

Can anyone help me? Here is my code.

Also when I nest the directory inside a lib directory it then tell me src/routes/mod.rs doesnt exist

Thank you!

File structure

.
├── Cargo.lock
├── Cargo.toml
├── README.md
├── src
│   ├── lib.rs
│   ├── main.rs
│   └── routes
│       ├── echo.rs
│       └── mod.rs
├── static
│   └── abruzzo.png
└── tests
    └── basic_test.rs

main.rs

mod lib;

fn main() {
    lib::rocket_builder().launch();
}

lib.rs

#![feature(proc_macro_hygiene, decl_macro)]
#![allow(unused_attributes)]

#[macro_use] use rocket::*;
use rocket_contrib::helmet::SpaceHelmet;

mod routes;

pub fn rocket_builder() -> rocket::Rocket {
    rocket::ignite().attach(SpaceHelmet::default())
    .mount("/", routes![routes::echo::echo_fn])

}

echo.rs

use rocket::*;

#[get("/echo/<echo>")]
pub fn echo_fn(echo: String) -> String {
    echo
}

mod.rs

pub mod echo;

Error:

error[E0583]: file not found for module `routes`
 --> src\lib.rs:8:1
  |
8 | mod routes;
  | ^^^^^^^^^^^
  |
  = help: to create the module `routes`, create file "src\lib\routes.rs" or "src\lib\routes\mod.rs"

error[E0433]: failed to resolve: could not find `echo` in `routes`
  --> src\lib.rs:12:33
   |
12 |     .mount("/", routes![routes::echo::echo_fn])
   |                                 ^^^^ could not find `echo` in `routes`

Some errors have detailed explanations: E0433, E0583.
For more information about an error, try `rustc --explain E0433`.
error: could not compile `rocket-mongo` due to 2 previous errors

Normally lib.rs is used as a library crate, not a module in the main executable. It should automatically be available as a dependency in your project, so remove that mod lib; and then main can call your_crate_name::rocket_builder().

4 Likes

The lower-level reason for your error is that lib.rs as a library crate can have its submodules in the same directory, whereas a module named lib must have its submodules under lib/...

3 Likes

Thank you so much that work! and the explaining makes sense. I am use to doing javascript and I think I took the fact that there is so much documentation on it for granted because I am having a hard time finding documentation sometimes for these error. I should also mention this is my first time at a lower level language

1 Like