How to structure a project of nested crates?

I'm writing a program that I'd like to structure as a collection of libraries with some dependencies between them and at least one executable with dependencies on those libraries. Does Cargo support this kind of project structure? Like can a Cargo.toml specify multiple child crates that are their own unique entities but are built by the parent? Or is there a different mechanism to do that?

1 Like

Yes, cargo can do this. You can use path attribute with a dependency to specify a local crate. Here are the docs: Page Moved

1 Like

Got it, thanks.

Does a Cargo.toml only provide the build rules for a single package (say library) or can it specify a bunch of libraries to build?

I'm not sure that I understand your question correctly, but cargo will build all dependencies automatically. That is, both remote (crates.io) and local (path) dependencies will be rebuild as necessary, and you don't need to do anything special for it besides declaring a dependency in Cargo.toml.

2 Likes

Here is an example of a project with several subcrates: GitHub - matklad/rustraytracer: A ray tracer written in Rust =)

2 Likes

It provides the stuff for 0-1 libraries and 0-N binaries. You'd use path to point at another crate, with its own Cargo.toml that describes how to build it.

2 Likes

Yes, thank you. That example is what I was looking for. :slight_smile: I got myself confused somewhere.

Do you have an example of building multiple binaries from a single Cargo.toml?

Edit: Or is that implicit in the src/bin/ directory?

There are two ways to have several binaries.

3 Likes