5

I have a Rust project with the structure:

.
├── Cargo.lock
├── Cargo.toml
├── src
│   ├── routes
│   │   ├── mod.rs
│   │   ├── router_get.rs
│   │   └── router_post.rs
│   ├── main.rs
│   └── server.rs

I need to use the routes module in server.rs, but when I am trying to compile it, it gives me an error:

error[E0432]: unresolved import `super::routes`
  --> src/server.rs:10:5
   |
10 | use super::routes;
   |     ^^^^^^^^^^ no `routes` in the root

When I try use routes in main.rs with mod routes, everything is ok. But I need to use it in server.rs.

routes/mod.rs

pub mod router_get;
pub mod router_post;
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Piotr Płaczek
  • 530
  • 1
  • 8
  • 20
  • Please search before you post a question; many questions about using modules has been asked and answered on here. To apply the answer to the other question: you need `mod routes;` in main.rs, *as well as* `use super::routes;` in server.rs. – trent Dec 11 '19 at 12:38
  • 1
    Also see [How do I import from a sibling module?](https://stackoverflow.com/questions/30677258/how-do-i-import-from-a-sibling-module) and [Why can't I import module from different file in same directory?](https://stackoverflow.com/q/55868434/3650362) – trent Dec 11 '19 at 12:40

1 Answers1

7

In your main.rs you've to load the module first.

mod routes;

fn main() {
}

In your server.rs just use

use crate::routes;
trent
  • 25,033
  • 7
  • 51
  • 90
createproblem
  • 1,542
  • 16
  • 30