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.
 
 
 
Go to file
Nick Cameron 65f12dcaab link ctor -> builder 9 years ago
idioms link ctor -> builder 9 years ago
patterns Builder pattern 9 years ago
LICENSE Initial commit 9 years ago
README.md link ctor -> builder 9 years ago
intro.md Initial commit 9 years ago
template.md Initial commit 9 years ago

README.md

Rust Design Patterns

An open source repository of design patterns and idioms in the Rust programming language.

Contents

Introduction

Idioms

  • Constructor
  • Concatenating strings with format!
  • private field to indicate extensibility
  • trait to separate visibility of methods from visibility of data (https://github.com/sfackler/rust-postgres/blob/master/src/lib.rs#L1400)
  • Deref on Vec/String to treat like a smart pointer/borrowed view of such data
  • 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")
  • dtor for finally
  • interior mutability - UnsafeCell, Cell, RefCell
  • treating Option like a list

Design patterns

Anti-patterns

  • thread + catch_panic for exceptions
  • Clone to satisfy the borrow checker
  • Deref polymorphism
  • Matching all fields of a struct (back compat)
  • wildcard matches
  • tkaing an enum rather than having multiple functions

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.

Correction and elaboration PRs are very welcome.