The format! macro constructs a String from formatted text instead of printing the result. It works like println!, but saves the finished text for later use. section 2.2
Combining names into a String
fn main() { let name = "Billybrobby"; let country = "USA"; let home = "Korea"; let together = format!("I am {name} from {country} but I live in {home}."); } section 2.2
The resulting value is stored in together, but nothing is printed until you pass that String to an output operation. This makes format! useful when formatted text must become data. section 2.2
A closure can also use format! to create a detailed error message, such as identifying which company has no CEO, before returning it as a String error. section 9.1.1
fn main() {
let name = "Billybrobby";
let country = "USA";
let home = "Korea";
let together = format!("I am {name} from {country} but I live in {home}.");
}
See the minimal example that interpolates three variables into one stored String. section 2.2
