--- url: "https://refactoring.guru/design-patterns/strategy/rust/example" titulek: "Strategy in Rust / Design Patterns" h1: "Strategy in Rust" popis: "Strategy pattern in Rust. Full code example in Rust with detailed comments and explanation. Strategy is a behavioral design pattern that turns a set of behaviors into objects and makes them interchangeable inside original context object." jazyk: "en" kanonicka: "https://refactoring.guru/design-patterns/strategy/rust/example" drobecky: ["Home", "Design Patterns", "Strategy", "Rust"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-13 22:22:58" znaku: 3487 slov: 493 kodovani: "utf-8" sha256: "60cee4009d46ad4cdbf3c23c5475babf2ca1b0831a53e1cf9d4515050b4ec38c" --- # **Strategy** in Rust **Strategy** is a behavioral design pattern that turns a set of behaviors into objects and makes them interchangeable inside original context object. The original object, called context, holds a reference to a strategy object. The context delegates executing the behavior to the linked strategy object. In order to change the way the context performs its work, other objects may replace the currently linked strategy object with another one. [Learn more about Strategy](/design-patterns/strategy) ## Conceptual Example A conceptual Strategy example via traits. #### **conceptual.rs** ``` /// Defines an injectable strategy for building routes. trait RouteStrategy { fn build_route(&self, from: &str, to: &str); } struct WalkingStrategy; impl RouteStrategy for WalkingStrategy { fn build_route(&self, from: &str, to: &str) { println!("Walking route from {} to {}: 4 km, 30 min", from, to); } } struct PublicTransportStrategy; impl RouteStrategy for PublicTransportStrategy { fn build_route(&self, from: &str, to: &str) { println!( "Public transport route from {} to {}: 3 km, 5 min", from, to ); } } struct Navigator { route_strategy: T, } impl Navigator { pub fn new(route_strategy: T) -> Self { Self { route_strategy } } pub fn route(&self, from: &str, to: &str) { self.route_strategy.build_route(from, to); } } fn main() { let navigator = Navigator::new(WalkingStrategy); navigator.route("Home", "Club"); navigator.route("Club", "Work"); let navigator = Navigator::new(PublicTransportStrategy); navigator.route("Home", "Club"); navigator.route("Club", "Work"); } ``` ### Output ``` Walking route from Home to Club: 4 km, 30 min Walking route from Club to Work: 4 km, 30 min Public transport route from Home to Club: 3 km, 5 min Public transport route from Club to Work: 3 km, 5 min ``` ## Functional approach Functions and closures simplify Strategy implementation as you can inject behavior right into the object without complex interface definition. It seems that Strategy is often implicitly and widely used in the modern development with Rust, e.g. it’s just like iterators work: ``` let a = [0i32, 1, 2]; let mut iter = a.iter().filter(|x| x.is_positive()); ``` #### **functional.rs** ``` type RouteStrategy = fn(from: &str, to: &str); fn walking_strategy(from: &str, to: &str) { println!("Walking route from {} to {}: 4 km, 30 min", from, to); } fn public_transport_strategy(from: &str, to: &str) { println!( "Public transport route from {} to {}: 3 km, 5 min", from, to ); } struct Navigator { route_strategy: RouteStrategy, } impl Navigator { pub fn new(route_strategy: RouteStrategy) -> Self { Self { route_strategy } } pub fn route(&self, from: &str, to: &str) { (self.route_strategy)(from, to); } } fn main() { let navigator = Navigator::new(walking_strategy); navigator.route("Home", "Club"); navigator.route("Club", "Work"); let navigator = Navigator::new(public_transport_strategy); navigator.route("Home", "Club"); navigator.route("Club", "Work"); let navigator = Navigator::new(|from, to| println!("Specific route from {} to {}", from, to)); navigator.route("Home", "Club"); navigator.route("Club", "Work"); } ``` ### Output ``` Walking route from Home to Club: 4 km, 30 min Walking route from Club to Work: 4 km, 30 min Public transport route from Home to Club: 3 km, 5 min Public transport route from Club to Work: 3 km, 5 min Specific route from Home to Club Specific route from Club to Work ``` ## **Strategy** in Other Languages