How can I append a formatted string to an existing String?

I see now that String implements Write, so we can use write!:

use std::fmt::Write;

pub fn main() {
    let mut a = "hello ".to_string();
    write!(a, "{}", 5).unwrap();

    println!("{}", a);
    assert_eq!("hello 5", a);
}

(Playground)

It is impossible for this write! call to return an Err, at least as of Rust 1.47, so the unwrap should not cause concern.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)