I've got following project structure, common, project1 and project2 are folders.
I am defining in cargo.toml two binaries main.rs in project1 and main.rs in project2
common
common1.rs
common.rs
mod.rs
project1
main.rs
project2
main.rs
I've defined few pub struct in common.rs and common1.rs but i can't import then in any of main.rs.
How can i accomplish that?
The normal and best way to share code between binaries is to define a library. Create src/lib.rs and put mod common; in it, then use it as use project name::common::common1::Whatever; from main.rs.
crate:: refers to the current crate, which is your binary. You need to use a path starting with the name of your library, which by default is equal to the [package] name = "..." in your Cargo.toml.
You don't need to declare a module inside its own file; each module only needs one mod. Doing this will result in you needing to use common::common:: to access its contents, because you've declared two nested modules that happen to have the same name. Just declare mod common; in lib.rs, and in common/mod.rs put the contents you want the common module to have.
Your path must start with the name of the library crate, not the name of any module.
If that doesn't clarify things, please post the full, unedited output of cargo check — there are many details in it which will help us understand exactly where the problem is. Also showing us exactly what files you currently have will help.
Sorry, i've made mistake writing, i've used name of the library defined in Cargo.toml under [package] name=... as you suggested
error[E0433]: failed to resolve: use of undeclared crate or module `project_name`
--> src/project1/main.rs:8:5
|
8 | use project_name::common::ClientBuilder;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ use of undeclared crate or module `project_name`