Reference Mods in other local packages

I have a set of Mod files in a workspace which I want to reference from another directory. But I cannot find the syntax I need to use. So the example is:

Primary
|_______ First
|            |_____ src
|                    |______ Cool_thing.rs
|_______ Second
            |_____ src
                    |______ main.rs

I want to reference a Struct in "Cool_things" from main.rs

In Primary cargo.toml I put something like:

[workspace]
members= ["First","Second", "Third"]

[dependencies.Second]
First= { path = "First" }

in the cargo.toml in the Second directory I have

[dependencies]
First = { path = "../First" }

and I have been trying things in my main.rs like:

extern crate First;
use First::Cool_things::Cool_things;

but no variation of use, mod, seem to allow me to reference the struct or impl or functions in Mod Cool_things.

You need a lib.rs in your First package with the following contents:

mod Cool_things;

That should cause use First::Cool_things::Cool_things to work in your other crate.

The important thing to remember is that mod is a module declaration, not an import. You should have exactly one mod statement for each module in your crate.

That worked. But broke other things. I'm wondering if I wouldn't be better off by putting all the Mod files into the top-level Primary/src directory and then accessing them from either First or Second?

So if Cool_things.rs is located in

Primary
|__ src

How do I tell the main.rs in Second and main.rs in First to look back up?

You would need to move the mod Cool_things statement into Primary's lib.rs and then make First and Second have a dependency on Primary instead of the other way around.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.