#Using another project in my project

17 messages · Page 1 of 1 (latest)

eager oxide
#

I'm trying to use another binary project in my project. This is my file structure:

. my_project
├── Cargo.toml
└── src
└── main.rs
└── other_project.rs

. other_project
├── Cargo.toml
└── src
└── main.rs
└── file1.rs
└── file2.rs

The use statements in other_project are giving me an error. other_project/src/file1.rs has "use crate::file2::Struct1" and the complier says "could not find file2 in crate root". I've tried changing the use statements to "use crate::other_project::file2::Struct1" but the error remains.

Following Ch.7 of the book I added:

  1. other_project = { path = "./other_project"} to my_project/Cargo.toml.
  2. “pub mod other_project;” to my_project/src/other_project.rs.
  3. “pub mod other_project” to my_project/src/main.rs
languid hare
#

I'm pretty sure you can't access exports from the main.rs, you need a lib.rs in other_project. Also if the projects are related, perhaps use a workspace instead?

#

Also you need to do other_project::file2 since you're importing it as a separate crate

#

Also I can't find the section of ch 7 that explains importing from other projects, only how to individual packages work

#

Could you link which part of ch 7 you're referring to?

eager oxide
#

other_project compiles fine by itself

#

I was trying to use other_project like garden in Ch.7

#

When I setup a workspace, I get the same error:

unresolved import other_project::file2
use crate::file2::Struct2
could not find file2 in the crate root

languid hare
#

Is file2 loaded as a module before the use statement in other_project?

languid hare
#

Modules in external files still need to be loaded with mod file_name for a module file_name.rs

eager oxide
# languid hare Well it needs to be

After I add “pub mod file2;” to other_project/src/file1.rs it says

to create module file2, create file “other_project/src/file1/file2.rs”

#

I don’t know what to put in that file

worldly rain
#

mod file2; should be in your src/main.rs or src/lib.rs, not in src/file1.rs

#

You declare modules where they exist in the project hierarchy, not where you use them