Skip to main content

String

Examples

let s = String::new();
s.push_str("123");

Q&A

What's the defference between `write!` and `push_str`?
  • write! directly writes to greetings
use std::fmt::Write;

let mut greetings = String::from("Hello");
let x: u8 = 10;
write!(greetings, " No.{}", x).unwrap();
  • push_str will allocate a String, get its ref and copy it to greetings, then throw it away
let mut greetings = String::from("Hello");
let x: u8 = 10;
greetings.push_str(&!format("{}", x));