0

according to https://stackoverflow.com/a/26435155/5884503, mod something searches for something.rs.

Here's what I'm trying to do based on that:

main.rs
message_socket.rs
mod.rs
body.rs

mod.rs:

mod message_socket;
mod body;

body.rs:

struct Body{}

message_socket.rs:

mod body;

error:

error[E0583]: file not found for module `body`
 --> src/message_socket.rs:7:1
  |
7 | mod body;
  | ^^^^^^^^^
  |
  = help: to create the module `body`, create file "src/message_socket/body.rs"

It's trying to look inside message_socket which is a file, not a directory.

Why?

loganfsmyth
  • 156,129
  • 30
  • 331
  • 251
Gatonito
  • 1,662
  • 5
  • 26
  • 55
  • Why are you putting `mod body;` inside `message_socket.rs` instead of in `mod.rs`? – loganfsmyth Mar 31 '21 at 02:36
  • @loganfsmyth I tried with and without it, in both I cannot do `use body::Body` in `message_socket.rs` – Gatonito Mar 31 '21 at 02:46
  • `mod.rs` only works when inside a subdirectory of `src`, not `src` itself. This sounds like it might be a [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), what are you trying to do? – 8176135 Mar 31 '21 at 02:57
  • @Prime_Aqasix I just want to import a sibling file, called `body.rs`, in `message_socket.rs` – Gatonito Mar 31 '21 at 03:06
  • **tl;dr:** All your top level module declarations should go in `main.rs`. To import a sibling module, you don't declare it again with `mod`; you just bring it into the current namespace with `use`. You don't need a `mod.rs` at all, and it's probably ignored in this example (you didn't show the contents of `main.rs`, which is the crate root). – trent Mar 31 '21 at 03:31
  • 1
    The duplicates suggest: 1) Put `mod message_socket; mod body;` in `main.rs` if it's not there already. 2) Put `use super::body;` (or `use crate::body;`) in `message_socket.rs`. 3) Delete `mod.rs`. – trent Mar 31 '21 at 03:37

0 Answers0