4
votes

I am a bit confused by the existence of the three upper levels in the Rust module hierarchy and why they are all needed. If I understand it correctly:

  • Crates are simple projects that contain multiple modules and each crate is either a library or a binary.
  • Packages contain one or more crates with a maximum of one library.
  • Workspaces are a group of packages.

I understand the need to keep several crates together if they are being developed together, so either packages or workspaces make sense to me, but I don't get why both need to exist and why there would need to be a maximum of 1 library restriction on packages. What are the advantages of doing it this way?

I have read Why can a Cargo package only have one library target? that gives an explanation for the 1 library crate per package rule, but it made me more confused, because you can still have packages with binaries and even "worse" no libraries. If packages are meant to be an abstraction for Cargo, the package manager, why allow binaries in them at all? Why allow packages without a single library inside? Can you import a no-library-package as a dependency?

1
To me, workspaces are an efficiency thing — they allow you to avoid recompiling the same dependencies when working in a related set of crates. - Shepmaster
Sure that makes sense to me. The part I don't understand is why have packages there at all. Have the workspaces be a collection of crates and that should suffice, no? What is gained by having the workspaces be a group of groups(packages) of crates? Having that intermediate level between projects(crates) and a grouping of related projects(workspaces) is the part I don't understand. - Xcode23
Packages == Rust 1.0. Workspaces == Rust 1.12. - Shepmaster
Ah so It's a legacy thing. That makes more sense. - Xcode23

1 Answers

0
votes

I've only just started with Rust, but as I see it a package with a library and binaries is basically just that a function specific library with tools (binaries), tools specific to that library.

Example - A 3D library for a game

Users of the library would be able to use the tools to for example create content, without having to pass on the tools to the end-user.