10

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?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Xcode23
  • 161
  • 2
  • 7
  • https://doc.rust-lang.org/cargo/reference/workspaces.html – Shepmaster May 27 '20 at 20:11
  • 3
    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 May 27 '20 at 20:11
  • 1
    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 May 27 '20 at 20:19
  • 1
    Packages == Rust 1.0. Workspaces == [Rust 1.12](https://blog.rust-lang.org/2016/09/29/Rust-1.12.html). – Shepmaster May 27 '20 at 20:30
  • Ah so It's a legacy thing. That makes more sense. – Xcode23 May 27 '20 at 20:34

1 Answers1

-2

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.

  • 2
    Thanks for your contribution! This doesn't seem to address workspaces at all; do you have anything to add about the difference between packages and workspaces? – trent Feb 23 '21 at 18:33
  • My apologies, it's been a long day... The Workspace contains the 'project' as a whole. Holding all relevant code and data in the form of 1 or more packages, along with additional work product and acts a container for the same. Of course as I said I've only just begun Rust and that's just my interpretation of the Workspace / Package relationship. – David Humphreys Feb 23 '21 at 22:52
  • ... My interpretation is based on this - https://doc.rust-lang.org/book/ch14-03-cargo-workspaces.html – David Humphreys Feb 23 '21 at 23:02