.github/workflows | ||
anti_patterns | ||
idioms | ||
patterns | ||
.env | ||
.gitignore | ||
book.toml | ||
intro.md | ||
LICENSE | ||
README.md | ||
SUMMARY.md | ||
template.md |
Rust Design Patterns
An open source repository of design patterns and idioms in the Rust programming language.
Contents
Idioms
- Constructor
- Concatenating strings with
format!
- Privacy for extensibility
- TODO stability for extensibility
- TODO trait to separate visibility of methods from visibility of data (https://github.com/sfackler/rust-postgres/blob/master/src/lib.rs#L1400)
- Collections are smart pointers
- TODO leak amplification ("Vec::drain sets the Vec's len to 0 prematurely so that mem::forgetting Drain "only" mem::forgets more stuff. instead of exposing uninitialized memory or having to update the len on every iteration")
- Finalisation in destructors
- TODO interior mutability - UnsafeCell, Cell, RefCell
- Iterating over an
Option
Default
trait- Pass variables to closure
mem::replace(_)
to avoid needless clones- Temporary mutability
- On-Stack Dynamic Dispatch
- TODO FFI usage (By being mindful of how to provide Rust libraries, and make use of existing libraries across the FFI, you can get more out of benefits Rust can bring)
- Easy doc initialization
Design patterns
- Builder
- RAII guards
- Newtype
- TODO iterators (to safely avoid bounds checks)
- TODO closures and lifetimes (coupling to lifetime)
- TODO platform-specific sub-modules (https://github.com/rust-lang/rfcs/blob/master/text/0517-io-os-reform.md#platform-specific-opt-in)
- TODO Module organisation (by looking at examples such as Rusts
libstd
, and how it integrated into the Rusts source code, lessons can be learned about ergonomic project management and API design. Closely assosciated with platform-specific sub-modules) - Entry API (TODO Currently just a boilerplate)
- Visitor
- Fold
- Prefer small crates
- Contain unsafety in small modules
- TODO extension traits
- TODO destructor bombs (ensure linear typing dynamically, e.g.,
0f25ccb7c3
) - TODO convertible to Foo trait for more generic generics (e.g., http://static.rust-lang.org/doc/master/std/fs/struct.File.html#method.open)
- Late bound bounds (Currently just a boilerplate)
- TODO 'shadow' borrowed version of struct - e.g., double buffering, Niko's parser generator
- TODO composition of structs to please the borrow checker
- TODO
Error
traits andResult
forwarding - TODO graphs
- Compose structs together for better borrowing
Anti-patterns
- TODO thread + catch_panic for exceptions
- TODO Clone to satisfy the borrow checker
- Deref polymorphism
- TODO Matching all fields of a struct (back compat)
- TODO wildcard matches
- TODO taking an enum rather than having multiple functions
- TODO
unwrap()
ing everyResult
instead of forwarding it #[deny(warnings)]
Contributing
Contributions are very welcome!
You should start with the template. Copy it into the appropriate directory, edit it, and submit a PR. You might not want every section, and you might want to add extra sections.
We suggest leaving a comment on the issue tracker so that other people don't start working on the same topic.
Correction and elaboration PRs are very welcome.
Building with mdbook
This book is built with mdbook. You can install it by running cargo install mdbook
.
If you want to build it locally you can run one of these two commands in the root directory of the repository:
-
mdbook build
Builds static html pages as output and place them in the
/book
directory by default. -
mdbook serve
Serves the book at
http://localhost:3000
(port is changeable, take a look at the terminal output to be sure) and reloads the browser when a change occurs.