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: Page Moved
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
.
Here is an example of a project with several subcrates: GitHub - matklad/rustraytracer: A ray tracer written in Rust =)
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 Cargo.toml
?
Edit: Or is that implicit in the src/bin/
directory?
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 auchat/src/bin at master · matklad/auchat · GitHub) - Explicitly, by listing
bin
s inCargo.toml
. This is great if the binary itself spans several files (example learnOpenGL/Cargo.toml at master · matklad/learnOpenGL · GitHub)