FAQ - Help and questions

How do I split a Rust project into several crates with a Cargo workspace?

#cargo#workspaces#toolingpublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

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].

Checked with rustc 1.93.0 and cargo 1.93.0, Windows 11

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

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.
Cargo.tomltoml
[workspace]
members = ["crates/*"]
resolver = "3"

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

[workspace.dependencies]
greeter = { path = "crates/greeter" }
  1. Create one folder per crate under crates/. Each member takes the shared settings with .workspace = true.
crates/greeter/Cargo.tomltoml
[package]
name = "greeter"
edition.workspace = true
version.workspace = true
crates/app/Cargo.tomltoml
[package]
name = "app"
edition.workspace = true
version.workspace = true

[dependencies]
greeter.workspace = true
  1. Put the code in the members.
crates/greeter/src/lib.rsrust
pub fn greet(name: &str) -> String {
    format!("Hello, {name}!")
}

#[cfg(test)]
mod tests {
    #[test]
    fn greets() {
        assert_eq!(super::greet("Ada"), "Hello, Ada!");
    }
}
crates/app/src/main.rsrust
fn main() {
    println!("{}", greeter::greet("workspace"));
}
  1. 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.

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.

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet