expression

Reuse a mutable builder for related configurations

A mutable builder lets the caller branch configurations from a shared setup. A non-consuming build must copy, clone, or borrow the retained fields; it cannot move a non-Copy field out of a shared borrow. Here independent configurations receive cloned labels. For large fields, consider a consuming build instead. Expected result (NOT RUN): cache: 2, cache: 4 Common mistake: Assuming build(&self) can move a String out of the builder or that cloning is free. Static Rust example; not a verified ByteMind executable assessment.

Formal Syntax & Structure

rust syntaxrust
fn capacity(&mut self, n: usize) -> &mut Self; fn build(&self) -> Config

Working Example

Example Snippetrust
struct Config { label: String, capacity: usize }struct Builder { label: String, capacity: usize }impl Builder {    fn capacity(&mut self, n: usize) -> &mut Self { self.capacity = n; self }    fn build(&self) -> Config { Config { label: self.label.clone(), capacity: self.capacity } }}fn main() {    let mut b = Builder { label: "cache".into(), capacity: 2 };    let a = b.build(); let c = b.capacity(4).build();    println!("{}: {}, {}: {}", a.label, a.capacity, c.label, c.capacity);}
Rust Reference