Multiple files, multiple binaries and a library?

⚓ Rust    📅 2025-08-07    👤 surdeus    👁️ 6      

surdeus

Hello,

I have a project with the following structure.

├── src
│ ├── algorithms
│ │ ├── file1.rs
│ │ ├── file2.rs
│ ├── bin
│ │ ├── main1.rs
│ │ ├── main2.rs
│ ├── lib.rs

On lib.rs:
pub mod algorithms {
pub mod file1;
pub mod file2;
}

On main1.rs:
use algorithms::file1::fnc1;

Cargo.toml:
[[bin]]
name = "main1"
path = "src/bin/main1.rs"

[[bin]]
name = "main2"
path = "src/bin/main2.rs"

Now, I want to create a library from that. So I added the following to my Cargo.toml:

[lib]
name = "mylib"
crate-type = ["cdylib"]

And now the "use" on main files cannot find the internal files.

How can I solve it? Te goal is to allow something like it to work:

cargo build --bin main1
cargo build --lib

4 posts - 2 participants

Read full topic

🏷️ Rust_feed