The on(type_pattern, attributes) attribute allows you to apply member attributes to all members that match a specific type pattern. This is useful for reducing boilerplate when you want to apply the same behavior (like into or required) to multiple fields in a struct, function parameters, or method arguments.
Usage Syntax
#[builder(on(type_pattern, attributes))]
You can also specify multiple on clauses, but they must be consecutive (no other attributes allowed between them).
Supported Attributes
into: Allows members to accept impl Into<T>.required: Makes members required (currently only works with the _ type pattern).setters(doc(default(skip))): Skips showing default values in generated documentation.overwritable: (Experimental) Allows calling setters for the same member multiple times. Requires the experimental-overwritable cargo feature.
#[derive(Builder)]
#[builder(on(String, into), on(PathBuf, into))]
struct Example {
name: String,
path: PathBuf,
level: u32,
}
Example::builder()
.name("accepts `impl Into<String>`")
.path("accepts/impl/into/PathBuf")
.level(100)
.build();