# How do I split a Rust project into several crates with a Cargo workspace?
Source: https://ruststudio.org/community/t/117/how-do-i-split-a-rust-project-into-several-crates-with-a-cargo-workspace
Author: Ruststudio Team (staff) | Published: 2026-12-05 | Updated: 2026-12-05 | Checked with: rustc 1.93.0 and cargo 1.93.0, Windows 11
Category: Help and questions | Kind: FAQ | Tags: cargo, workspaces, tooling | Views: 0

## Short answer
A workspace is a root Cargo.toml with a [workspace] table that lists member crates. The members share one Cargo.lock and one target folder, can depend on each other by path, and can share dependency versions and package settings through [workspace.dependencies] and [workspace.package].

## Answer
## Answer
Use a workspace when one project grows into a library plus programs that use it, or into parts that should compile and be tested separately. All members build into one `target` folder, so shared dependencies are compiled once, and one `Cargo.lock` keeps their versions consistent.

## How to do it
1. Make a folder with a root `Cargo.toml` that has no `[package]`, only the workspace.

```toml title=Cargo.toml
[workspace]
members = ["crates/*"]
resolver = "3"

[workspace.package]
edition = "2024"
version = "0.1.0"

[workspace.dependencies]
greeter = { path = "crates/greeter" }
```

2. Create one folder per crate under `crates/`. Each member takes the shared settings with `.workspace = true`.

```toml title=crates/greeter/Cargo.toml
[package]
name = "greeter"
edition.workspace = true
version.workspace = true
```

```toml title=crates/app/Cargo.toml
[package]
name = "app"
edition.workspace = true
version.workspace = true

[dependencies]
greeter.workspace = true
```

3. Put the code in the members.

```rust title=crates/greeter/src/lib.rs
pub fn greet(name: &str) -> String {
    format!("Hello, {name}!")
}

#[cfg(test)]
mod tests {
    #[test]
    fn greets() {
        assert_eq!(super::greet("Ada"), "Hello, Ada!");
    }
}
```

```rust title=crates/app/src/main.rs
fn main() {
    println!("{}", greeter::greet("workspace"));
}
```

4. Run a member with `-p`, and test everything with `--workspace`.

```text
> cargo run -q -p app
Hello, workspace!
> cargo test --workspace
     Running unittests src\main.rs
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
     Running unittests src\lib.rs
test tests::greets ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
   Doc-tests greeter
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
```

## Why it works
Cargo looks upward from the current folder for a `Cargo.toml` with a `[workspace]` table, so every command works from any member's folder. `[workspace.dependencies]` states a version or path once; members opt in with `name.workspace = true`, so one crate cannot quietly drift to another version.

## Common mistakes
- Putting `[package]` and `[workspace]` in the same root file when the root itself is not meant to be a crate. That is allowed (a "root package"), but it surprises people; a virtual workspace as above is simpler.
- Forgetting that `cargo run` with several binaries needs `-p name` (or `--bin name`) to pick one.
- Running `cargo test` in the root and expecting every member to be tested. In a virtual workspace Cargo tests the default members; `--workspace` is explicit.

## Related questions
### Do all members need the same version number?
No. `version.workspace = true` is a convenience. A member may set its own version.

### Can a workspace member depend on a crate from crates.io and from the workspace at once?
Yes. Path dependencies and registry dependencies sit side by side in the same `[dependencies]` table.
