MCPcopy Index your code
hub / github.com/fadeevab/design-patterns-rust

github.com/fadeevab/design-patterns-rust @main

Chat with this repo
repository ↗ · DeepWiki ↗ · + Follow
396 symbols 631 edges 112 files 41 documented · 10% updated 18mo ago★ 1,058

Browse by type

Functions 277 Types & classes 119
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

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.82 (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-top-down
cargo run --bin mediator-rc-refcell
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-local
cargo run --bin singleton-lazy
cargo run --bin singleton-mutex
cargo run --bin singleton-once
cargo run --bin singleton-logger
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).

Extension points exported contracts — how you extend this code

Core symbols most depended-on inside this repo

Shape

Method 216
Class 85
Function 61
Interface 29
Enum 5

Languages

Rust100%

Modules by API surface

creational/builder/components.rs13 symbols
behavioral/template-method/main.rs13 symbols
behavioral/visitor/main.rs11 symbols
behavioral/state/player.rs11 symbols
structural/bridge/device/tv.rs10 symbols
structural/bridge/device/radio.rs10 symbols
creational/singleton/logger/log.rs9 symbols
creational/builder/cars/car.rs9 symbols
behavioral/state/state.rs9 symbols
behavioral/strategy/conceptual.rs8 symbols
structural/bridge/remotes/mod.rs7 symbols
creational/builder/builders/car_manual.rs7 symbols

For agents

$ claude mcp add design-patterns-rust \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page