Into
conversions
If you have members of type String
, or PathBuf
, and you need to set them to a hard-coded string literal, then you have to write .to_owned()
or .to_string()
or .into()
.
rust
use bon::Builder;
use std::path::PathBuf;
#[derive(Builder)]
struct Project {
name: String,
description: String,
path: PathBuf,
}
Project::builder()
.name("Bon".to_owned())
.description("Awesome crate 🐱".to_string())
.path("/path/to/bon".into())
.build();
However, you can ask bon
to generate setters that accept impl Into<T>
to remove the need for manual conversion.
This can be configured with #[builder(into)]
for a single member or with #[builder(on({type}, into))]
for many members at once.
rust
use bon::Builder;
use std::path::PathBuf;
// All setters for members of type `String` will accept `impl Into<String>`
#[derive(Builder)]
#[builder(on(String, into))]
struct Project {
name: String,
description: String,
// The setter only for this member will accept `impl Into<PathBuf>`
#[builder(into)]
path: PathBuf,
}
Project::builder()
.name("Bon")
.description("Awesome crate 🐱")
.path("/path/to/your/heart")
.build();
Into
conversions don't always make sense, and you should be aware of their downsides as well. The article Into Conversions In-Depth provides recommendations on when it makes sense to use and to avoid Into
conversions.