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?
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
Got it, thanks.
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
Here is an example of a project with several subcrates: https://github.com/matklad/rustraytracer =)
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.
Yes, thank you. That example is what I was looking for. I got myself confused somewhere.
Do you have an example of building multiple binaries from a single
Edit: Or is that implicit in the
There are two ways to have several binaries.
- Implicitly, every file in
src/bin/is a single binary. It’s great for a bunch of one file binaries (example https://github.com/matklad/auchat/tree/master/src/bin)
- Explicitly, by listing
Cargo.toml. This is great if the binary itself spans several files (example https://github.com/matklad/learnOpenGL/blob/master/Cargo.toml#L25-L39)