macro

format! — Build an owned formatted string

Create an owned String instead of printing. This is useful when a message must be returned, stored or passed to another API. Formatting uses the traits selected by the format placeholders. Expression result: String. Expected result (not executed): stdout: 'Tea: 250 cents\n'; no stderr. Common mistake: Discarding the returned String and expecting terminal output.

Formal Syntax & Structure

rust syntaxrust
format!("format string", arguments...)

Working Example

Example Snippetrust
fn main() {    let name = "Tea";    let cents = 250;    let label = format!("{name}: {} cents", cents);    println!("{label}");}
Rust Reference