Giter Site home page Giter Site logo

design-patterns-rust's Introduction

Design Patterns in Rust

Design Patterns in Rust

This repository contains Rust ๐Ÿฆ€ examples for all 23 classic GoF design patterns, and even a little more.

All examples are designed to introduce practical applicability in the Rust language. There are conceptual and real-world examples. In both cases, Rust idiomatic ways of code development and all the specifics are taken into account.

The repository is developed to be a part of the Refactoring.Guru project.

๐Ÿ”ง Requirements

These examples have been tested with a stable rustc 1.62 (2021 edition).

All examples can be launched via the command line, using cargo as follows:

cargo run --bin adapter

Each target name can be found in Cargo.toml of each example:

[[bin]]
name = "adapter"
path = "main.rs"

Also, the examples contain a README.md with instructions and additional explanations.

โœ… List of Examples

cargo run --bin chain-of-responsibility
cargo run --bin command
cargo run --bin iterator
cargo run --bin mediator
cargo run --bin memento
cargo run --bin memento-serde
cargo run --bin observer
cargo run --bin state
cargo run --bin strategy
cargo run --bin strategy-func
cargo run --bin template-method
cargo run --bin visitor
cargo run --bin abstract-factory
cargo run --bin abstract-factory-dyn
cargo run --bin builder
cargo run --bin factory-method-maze-game
cargo run --bin factory-method-render-dialog
cargo run --bin prototype
cargo run --bin simple-factory
cargo run --bin singleton
cargo run --bin singleton-lazy
cargo run --bin singleton-mutex # Requires Rust 1.63
cargo run --bin static-creation-method
cargo run --bin adapter
cargo run --bin bridge
cargo run --bin composite
cargo run --bin decorator
cargo run --bin facade
cargo run --bin flyweight
cargo run --bin proxy

Some examples have visual output.

Flyweight State Command

๐Ÿ’ก Notes

Interestingly, in Rust:

  1. Almost all structural and creational patterns can be implemented using generics, hence, static dispatch.
  2. Most behavioral patterns can NOT be implemented using static dispatch, instead, they can be implemented only via dynamic dispatch.

A well-thought pattern classification fits the Rust language design perfectly as "behavior" is dynamic in nature and "structure" is static.

Some patterns are really easy to implement in Rust, mostly creational ones, e.g. Prototype, Static Creation Method.

The Mediator behavioral pattern is the hardest to implement with Rust, considering Rust's specific ownership model with strict borrow checker rules.

License

This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.

Creative Commons License

Credits

Authors: Alexander Fadeev (@fadeevab).

design-patterns-rust's People

Contributors

dblm0 avatar fadeevab avatar flrnfrmm avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.