How to structure a project of nested crates?


#1

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?


#2

Yes, cargo can do this. You can use path attribute with a dependency to specify a local crate. Here are the docs: http://doc.crates.io/manifest.html#the-dependencies-section


#3

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?


#4

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.


#5

Here is an example of a project with several subcrates: https://github.com/matklad/rustraytracer =)


#6

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.


#7

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


#8

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

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


#9

There are two ways to have several binaries.