You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.9 KiB
Markdown
48 lines
1.9 KiB
Markdown
8 years ago
|
# Prefer small crates
|
||
|
|
||
|
## Description
|
||
|
|
||
|
Prefer small crates that do one thing well.
|
||
|
|
||
3 years ago
|
Cargo and crates.io make it easy to add third-party libraries, much more so than
|
||
|
in say C or C++. Moreover, since packages on crates.io cannot be edited or removed
|
||
|
after publication, any build that works now should continue to work in the future.
|
||
3 years ago
|
We should take advantage of this tooling, and use smaller, more fine-grained dependencies.
|
||
8 years ago
|
|
||
|
## Advantages
|
||
|
|
||
1 year ago
|
- Small crates are easier to understand, and encourage more modular code.
|
||
|
- Crates allow for re-using code between projects.
|
||
3 years ago
|
For example, the `url` crate was developed as part of the Servo browser engine,
|
||
3 years ago
|
but has since found wide use outside the project.
|
||
1 year ago
|
- Since the compilation unit
|
||
3 years ago
|
of Rust is the crate, splitting a project into multiple crates can allow more of
|
||
|
the code to be built in parallel.
|
||
8 years ago
|
|
||
|
## Disadvantages
|
||
|
|
||
1 year ago
|
- This can lead to "dependency hell", when a project depends on multiple conflicting
|
||
3 years ago
|
versions of a crate at the same time. For example, the `url` crate has both versions
|
||
|
1.0 and 0.5. Since the `Url` from `url:1.0` and the `Url` from `url:0.5` are
|
||
|
different types, an HTTP client that uses `url:0.5` would not accept `Url` values
|
||
|
from a web scraper that uses `url:1.0`.
|
||
1 year ago
|
- Packages on crates.io are not curated. A crate may be poorly written, have
|
||
3 years ago
|
unhelpful documentation, or be outright malicious.
|
||
1 year ago
|
- Two small crates may be less optimized than one large one, since the compiler
|
||
3 years ago
|
does not perform link-time optimization (LTO) by default.
|
||
8 years ago
|
|
||
|
## Examples
|
||
|
|
||
3 years ago
|
The [`ref_slice`](https://crates.io/crates/ref_slice) crate provides functions
|
||
|
for converting `&T` to `&[T]`.
|
||
8 years ago
|
|
||
3 years ago
|
The [`url`](https://crates.io/crates/url) crate provides tools for working with
|
||
|
URLs.
|
||
8 years ago
|
|
||
3 years ago
|
The [`num_cpus`](https://crates.io/crates/num_cpus) crate provides a function to
|
||
|
query the number of CPUs on a machine.
|
||
8 years ago
|
|
||
|
## See also
|
||
|
|
||
1 year ago
|
- [crates.io: The Rust community crate host](https://crates.io/)
|