

Weekly Rust Trivia: How to Implement the Builder Pattern
source link: https://www.thorsten-hans.com/weekly-rust-trivia-implement-the-builder-pattern/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Weekly Rust Trivia: How to Implement the Builder Pattern
Published Thu, Jul 6, 2023 / by Thorsten Hans / Estimated reading time: 2 min
Weekly Rust Trivia is a problem-oriented series of articles that assist developers while learning Rust. Every article solves simple, everyday development tasks using the Rust standard library or leveraging popular and proven crates.
Question: How to implement the builder pattern in Rust?
The derive_builder
crate provides a macro which allows us to satisfy the builder pattern for your individual structs without writing custom code.
First let’s add the dependency using cargo add
:
# Add derive_builder as dependency
cargo add derive_builder
Having the crate added to our project, we can move on and implement the builder pattern for the fictive struct Car
:
use derive_builder::Builder;
#[derive(Builder, Debug)]
struct Car {
color: String,
transmission: Transmission,
convertible: bool,
mileage: u32,
}
#[derive(Clone, Debug)]
enum Transmission {
Manual,
SemiAuto,
Automatic,
}
The code defines a struct called Car
and an enum called Transmission
. The Car
struct is annotated with the Builder
derive macro from the derive_builder
crate. The Builder
macro generates a builder pattern implementation for the Car
struct - by automatically generating another struct called CarBuilder
- allowing users to construct new Car
instances with optional or defaulted fields in a fluent style. The Transmission
enum defines three variants: Manual
, SemiAuto
, and Automatic
.
We can use the automatically generated CarBuilder
as shown here:
fn main() -> Result<(), Box<dyn std::error::Error>>{
let car = CarBuilder::default()
.color("red".to_string())
.transmission(Transmission::Manual)
.convertible(false)
.mileage(10000)
.build()?;
println!("{:#?}", car);
Ok(())
}
Which will print the debug representation of car
:
# Car {
# color: "red",
# transmission: Manual,
# convertible: false,
# mileage: 10000,
#}
If you want to dive deeper into implementing the builder pattern in Rust, and see how to craft the builder pattern manually - without adding the derive_builder
crate to your project, consider reading “Design Patterns in Rust: An Introduction to the Builder Pattern” from Engin Diri.
Recommend
-
9
Weekly Rust Trivia: How to Get all Files in a Directory Published Thu, May 11, 2023 / by
-
8
Weekly Rust Trivia: How to Download an Image to a File Published Thu, May 18, 2023 / by
-
4
Weekly Rust Trivia: How to Compute a SHA256 Hash of a File Published Thu, May 25, 2023 / by
-
8
Weekly Rust Trivia: How to Read a CSV File Published Thu, Jun 1, 2023 / by Thorsten Ha...
-
4
Weekly Rust Trivia: How to Implement Binary Search Published Thu, Jun 8, 2023 / by Tho...
-
4
Weekly Rust Trivia: How to create a TCP Server Published Thu, Jun 15, 2023 / by Thorst...
-
5
Weekly Rust Trivia: How to Retrieve Image Dimensions Published Thu, Jun 22, 2023 / by ...
-
9
Weekly Rust Trivia: How to Implement a Generic Stack Published Thu, Jun 29, 2023 / by ...
-
5
Weekly Rust Trivia: How to write a function-like macro Published Thu, Jul 20, 2023 / by
-
10
Weekly Rust Trivia: How to use pattern matching Published Thu, Aug 3, 2023 / by Thorst...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK