0

Let's assume we have the following file tree:

src
|--bin
|   |--hw.rs
|--libs
|   |--streams.rs
|   |--structs.rs
|--main.rs

I want to access some structs from the structs module inside the hw file, however it doesn't compile, as it can't find lib::structs whatever I do.

The main.rs file contains the following:

mod streams;
mod structs;
use streams::{X, Y, Z};
use structs::{A, B, C, D, E};
(some more code)

The streams.rs file contains the following:

use crate::structs::{A, B, C, D, E};
(some more code)

The structs.rs file contains the following:

use crate::structs::{A, B, C, D, E};
(some more code)

The hw.rs file contains the following:

use crate::libs::structs::{A, B};
use polars::prelude::*;
(some more code)

This throws the following Error:

error[E0433]: failed to resolve: could not find `libs` in the crate root
 --> src/bin/hw.rs:7:12
  |
7 | use crate::libs::structs::{A, B};
  |            ^^^^ could not find `libs` in the crate root

I have tried to add the following at the top of hw.rs

// #[path = "../libs"]
// mod streams;
// mod structs;

It didn't work, I tried using the super keyword, it didn't work. Generally, every solution I've tried the past 2 days (removing crate from the import line, trying to access structs directly etc.) that worked for others didn't work for me. I also tried to create a workspace in case I could work around this issue but still had the same problem.

So? What am I doing wrong, and thus, what have I not understood correctly?

AT3RM0N
  • 1
  • 2
  • Adapting from an answer in the first link: `mod streams;` for example, doesn't work in src/lib.rs because this tries to create the submodules in the crate's root module, which would have to exist in either `src/streams.rs` or `src/streams/mod.rs` -- you put the file in `src/libs/streams.rs` instead. You need to rearrange the source files in the file system. See the other linked questions for more tips. – E_net4 May 24 '23 at 10:24
  • @Enet4 I did change the files streams.rs and structs.rs locations and moved them to src (where main.rs is). However I still cannot access them from hw.rs. I think the hotel example you mentioned is kind of different, if I'm not mistaken. What do I need to include on top of hw.rs to access components from structs.rs? Thank you in advance for your time :) – AT3RM0N May 24 '23 at 11:09
  • That would be another issue indeed: since the entry point is a file named `main.rs`, the project contains a main binary crate, plus a secondary binary (`hw`). I do not believe that one can access those modules without redeclaring them independently in `bin/hw.rs`, _unless_ `streams` and `structs` are moved into a _library_ crate in the same project. See [Package with both a library and a binary](https://stackoverflow.com/questions/26946646/package-with-both-a-library-and-a-binary) to know how to proceed. Then you would be able to `use crate::...` in both binaries. – E_net4 May 24 '23 at 11:21

0 Answers0