--- url: "https://refactoring.guru/design-patterns/template-method/rust/example" titulek: "Template Method in Rust / Design Patterns" h1: "Template Method in Rust" popis: "Template Method pattern in Rust. Full code example in Rust with detailed comments and explanation. Template Method is a behavioral design pattern that allows you to define a skeleton of an algorithm in a base class and let subclasses override the steps without changing the overall algorithm’s structure." jazyk: "en" kanonicka: "https://refactoring.guru/design-patterns/template-method/rust/example" drobecky: ["Home", "Design Patterns", "Template Method", "Rust"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-13 22:23:11" znaku: 2558 slov: 297 kodovani: "utf-8" sha256: "79955cde8e652708e95868643fc885f28df9c65894ea4cf697f96445f9837c3e" --- # **Template Method** in Rust **Template Method** is a behavioral design pattern that allows you to define a skeleton of an algorithm in a base class and let subclasses override the steps without changing the overall algorithm’s structure. [Learn more about Template Method](/design-patterns/template-method) ## Conceptual Example #### **main.rs** ``` trait TemplateMethod { fn template_method(&self) { self.base_operation1(); self.required_operations1(); self.base_operation2(); self.hook1(); self.required_operations2(); self.base_operation3(); self.hook2(); } fn base_operation1(&self) { println!("TemplateMethod says: I am doing the bulk of the work"); } fn base_operation2(&self) { println!("TemplateMethod says: But I let subclasses override some operations"); } fn base_operation3(&self) { println!("TemplateMethod says: But I am doing the bulk of the work anyway"); } fn hook1(&self) {} fn hook2(&self) {} fn required_operations1(&self); fn required_operations2(&self); } struct ConcreteStruct1; impl TemplateMethod for ConcreteStruct1 { fn required_operations1(&self) { println!("ConcreteStruct1 says: Implemented Operation1") } fn required_operations2(&self) { println!("ConcreteStruct1 says: Implemented Operation2") } } struct ConcreteStruct2; impl TemplateMethod for ConcreteStruct2 { fn required_operations1(&self) { println!("ConcreteStruct2 says: Implemented Operation1") } fn required_operations2(&self) { println!("ConcreteStruct2 says: Implemented Operation2") } } fn client_code(concrete: impl TemplateMethod) { concrete.template_method() } fn main() { println!("Same client code can work with different concrete implementations:"); client_code(ConcreteStruct1); println!(); println!("Same client code can work with different concrete implementations:"); client_code(ConcreteStruct2); } ``` ### Output ``` Same client code can work with different concrete implementations: TemplateMethod says: I am doing the bulk of the work ConcreteStruct1 says: Implemented Operation1 TemplateMethod says: But I let subclasses override some operations ConcreteStruct1 says: Implemented Operation2 TemplateMethod says: But I am doing the bulk of the work anyway Same client code can work with different concrete implementations: TemplateMethod says: I am doing the bulk of the work ConcreteStruct2 says: Implemented Operation1 TemplateMethod says: But I let subclasses override some operations ConcreteStruct2 says: Implemented Operation2 TemplateMethod says: But I am doing the bulk of the work anyway ``` ## **Template Method** in Other Languages