Why does Rust need both packages and workspaces?

Viewed 1035

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 Answers

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.

Related