What's the best way to structure a project with multiple services and a common library?

I'm about to start a new project that will have three http and udp services that will all share some common code. In C# I would just create a project solution and add new projects to it, one of them being a common library. What is the most common way this is done in the Rust community? Do we use workspaces for this?

I have done this a few times. Try this structure for a project:

my_project
__Cargo.toml
__/src
____lib.rs
____foo.rs
____bar.rs
____/bin
______program_1.rs
______program_2.rs

Then in your Cargo.toml, use

#[workspace]

[lib]
name = "my_project"
path = "src/lib.rs"

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

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

When it comes time to compile, test, run, etc,. use something like

cargo run --bin program_1
cargo build --release --bin program_2

Note that in foo.rs, you would import bar.rs with

use crate::bar;

Whereas in program_1.rs, program_2.rs you import bar.rs with

use my_project::bar;

I definitely don't want all the source code in the same project. While the services are similar, they do need distinct separation, not only for my own sanity, but for my teammate's sanity as well.

Oh I think I misunderstood. You can make a folder with its own rust crate and import it locally.

big_group_project
__Cargo.toml
__/src
__/jeramys_rust_crate
____Cargo.toml
____/src
__/some_other_local_crate
____Cargo.toml
____/src

From the "parent" Cargo.toml, you can import jeramys_rust_crate with

jeramys_rust_crate = { path = "jeramys_rust_crate"}

I know this works for local subdirectories. I have not tried it with absolute paths.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.