On this page
format
Prev: `Error` | Up: error_and_fmt | Next: ffi
Rel: error_and_fmt, rust/builtins
Ref: Rust std — format
Tags: #public #prog #rust #stdlib #error_and_fmt #tier/core
Signature & Overview
format!("user={name}, count={count}")
Macro returning a newly allocated String with compile-time checked format arguments.
Notes & Pitfalls
- Always heap-allocates a new
String: InsideDisplay::fmtor tight I/O loops, preferwrite!(buf, ...)to avoid temporary allocations. - Supports inline variable capture:
format!("{name}: {score:.1}")works since Rust 1.58.
Example
fn main() {
let name = "Ada";
let score = 98.5;
let s = format!("{name} scored {score:.1}%");
assert_eq!(s, "Ada scored 98.5%");
}
Linked from
Related notes
- tuts/rust/std/error_and_fmt/Error Linked here; links to this page; shared tags: #prog, #rust, #stdlib, #tier
- tuts/rust/std/error_and_fmt Linked here; links to this page; shared tags: #prog, #rust, #stdlib
- tuts/rust/std/ffi Linked here; shared tags: #prog, #rust, #stdlib
- tuts/rust/builtins Linked here; shared tags: #prog, #rust
- tuts/rust/std/collections/BinaryHeap shared tags: #prog, #rust, #stdlib, #tier
- tuts/rust/std/collections/BTreeMap shared tags: #prog, #rust, #stdlib, #tier
- tuts/rust/std/collections/HashMap shared tags: #prog, #rust, #stdlib, #tier
- tuts/rust/std/collections/HashSet shared tags: #prog, #rust, #stdlib, #tier
- tuts/rust/std/collections/VecDeque shared tags: #prog, #rust, #stdlib, #tier
- tuts/rust/std/error_and_fmt/Display shared tags: #prog, #rust, #stdlib, #tier