finish_fn
​
Applies to: structs functions methods
Overrides name, visibility and docs for the finishing function.
TIP
Don't confuse this with the member-level #[builder(finish_fn)]
attribute.
Short syntax configures just the name.
attr
#[builder(finish_fn = custom_name)]
Long syntax provides more flexibility. All keys are optional.
attr
#[builder(
finish_fn(
name = custom_name,
vis = "pub(crate)",
doc {
/// Custom docs
}
)
)]
name
​
The default name for the finishing function is chosen according to the following rules:
Syntax | Default |
---|---|
struct T | build |
Associated fn T::new/builder() | build |
Associated fn T::fn_name() | call |
Free fn fn_name() | call |
vis
​
The visibility must be enclosed with quotes. Use ""
or "pub(self)"
for private visibility.
The default visibility is the same as the visibility of the builder_type
, which in turn, defaults to the visibility of the underlying struct
or fn
.
doc
​
Simple documentation is generated by default. The syntax of this attribute expects a block with doc comments.
attr
doc {
/// Doc comments
}
Examples ​
rust
use bon::Builder;
#[derive(Builder)]
#[builder(finish_fn = assemble)]
struct Article {
id: u32
}
let article = Article::builder()
.id(42)
.assemble();
assert_eq!(article.id, 42);
rust
use bon::builder;
#[builder(finish_fn = send)]
fn get_article(id: u32) -> String {
format!("Some article with id {id}")
}
let response = get_article()
.id(42)
.send();
assert_eq!(response, "Some article with id 42");
rust
use bon::bon;
struct ArticlesClient;
#[bon]
impl ArticlesClient {
#[builder(finish_fn = send)]
fn get_article(&self, id: u32) -> String {
format!("Some article with id {id}")
}
}
let response = ArticlesClient
.get_article()
.id(42)
.send();
assert_eq!(response, "Some article with id 42");