How are dependencies merged?

I'm not totally clear on how dependencies are merged into a final binary.

By way of example, let's say there's a binary that depends on two crates A and B

Furthermore, A depends on C with some features, and the binary also depends on C but with other features, and B depends on C with the same features as A but a different minor version.

In what situations will C be duplicated and bloat the final binary size vs. be re-used across all the things that depend on it?

I'm looking more for a general understanding of how features and versions affect this, rather than the specific answer to the example here. Thanks!

2 Likes

To understand the answer to this question, you first need to know what "semver compatible" means. You can read about that here:

https://doc.rust-lang.org/cargo/reference/resolver.html#semver-compatibility

Once you understand this, the answer is the following:

Semver compatible versions of the same library are never duplicated. Semver incompatible versions of the same library are always duplicated. If you depend on multiple features of several semver compatible versions of the same library, then it will be compiled with the union of all of those features.

1 Like