Macro vec

Source
macro_rules! vec {
    () => { ... };
    ($($item:expr),+ $(,)?) => { ... };
}
Expand description

Same as std::vec! but converts each element with Into.

WARNING: it’s not recommended to import this macro into scope. Reference it using the full path (bon::vec![]) to avoid confusion with the std::vec! macro.

A good example of the use case for this macro is when you want to create a Vec<String> where part of the items are hard-coded string literals of type &str and the other part is made of dynamic String values.

fn convert_media(input_extension: &str, output_extension: &str) -> std::io::Result<()> {
    let ffmpeg_args: Vec<String> = bon::vec![
        "-i",
        format!("input.{input_extension}"),
        "-y",
        format!("output.{output_extension}"),
    ];

    std::process::Command::new("ffmpeg").args(ffmpeg_args).output()?;

    Ok(())
}

This macro doesn’t support vec![expr; N] syntax, since it’s simpler to just write vec![expr.into(); N] using std::vec! instead.