#builder #builder-pattern #macro #define #str #immutability #movie

macro assemblist

Define your builder patterns as you use them

1 unstable release

new 0.1.0 Mar 16, 2025

#2006 in Rust patterns

42 downloads per month

MIT license

52KB
1.5K SLoC

Easily create immutable builders in Rust.

The builder pattern is encouraged in the Rust language, in particular as a strategy to emulate named and optional arguments, which are intentionally not supported by Rust. However creating all the builder machinery can lead to boilerplate code, in particular when the generated data is complex and multi-layered. The usual builder pattern is based on mutation and generally turns compile-time checks that the final object is complete to a runtime verification. Assemblist allows you to create immutable builders structured as method chains like in

fn define_movie<'a>(name: &'a str)
    .released_in(release_year: usize)
    .directed_by(director_name: &'a str) -> Movie
{
    Movie {
        name: name.to_string(),
        release_year,
        director_name: director_name.to_string(),
    }
}

You can then just call it as it was declared

let movie = define_movie("The Lobster")
    .released_in(2015)
    .directed_by("Yorgos Lanthimos");

Dependencies

~205–640KB
~15K SLoC