What is the idiomatic way to manage a project with multiple crates?

A long time ago I messed with small toy projects with Rust. I am now preparing to start working on a pretty complex project that I plan to have split up into multiple crates (both for supposed compile time advantages and the hope later I am able to open source most (or all) of the crates.

However,I am not finding much information about how to organize a project with multiple crates. I did find some references to a workspace feature that is new and does not seem to be released yet (merged a few weeks ago only).

Is there an idiomatic way to do this?

2 Likes

I think that there are two popular conventions.

All crates live in the same directory and depend on each other via path = "../other-crate/".

lib-foo/
    Cargo.toml
    src/

lib-bar/
    Cargo.toml
    src/

lib-main/
    Cargo.toml
    src/

Example: https://github.com/nikomatsakis/lalrpop

Another convention is that there is one main crate, which have dependencies of the form { path = ./other-crate}

Cargo.toml
src/

lib-bar/
    Cargo.toml
    src/

lib-foo/
    Cargo.toml
    src/

Example: https://github.com/google/xi-editor/tree/master/rust

as a variation, sub crates might live in the special directory (my personal preference):

Cargo.toml
src/

lib/
    bar/
        Cargo.toml
        src/

    foo/
        Cargo.toml
        src/
3 Likes

I don't know about idiomatic, but here are a few projects that contain multiple crates as subdirectories:

2 Likes

Also, Servo has a components directory containing some ~20 crates.

1 Like

You may want to also check out workspaces Page Moved

2 Likes

I saw workspaces but my understanding is it hasn't made it out of Nightly yet.