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


#1

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

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

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


#4

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


#5

You may want to also check out workspaces http://doc.crates.io/manifest.html#the-workspace-section


#6

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