6 Designing a Library
This chapter covers
- Thinking about how to design a great library
- Making beautiful interfaces
- Correctness and avoiding unexpected behaviour
- Rust library ergonomics and patterns
This chapter marks the approximate halfway point in this book, so we’ll take a slight departure from the other content to discuss a subjective and somewhat controversial subject: what constitutes good library design. There’s no controversy that good design is better than bad design, but few can agree subjectively on what constitutes good. The zeitgeist of opinion surrounding good versus bad tends to shift and swing over time, which is important to consider for your designs.
The truth about good software design is that there are few universal rules, and much of what constitutes good is a matter of fashion, context, availability and quality of tooling, as well as how the human-computer interface (which–in the context of APIs–is the source code) functions across these dimensions.
In this chapter, we’ll explore at a higher level some of the ideas, processes, and methods one should consider when designing a library, with the end goal of producing a library that strikes the right balance between being easy to use, delightful to work with, difficult to use incorrectly, and flexible enough to solve a wide variety of problems. We’ll use an example from earlier in the book to build our library.