Show Menu
Cheatography

Rust std::fmt Cheat Sheet (DRAFT) by

Rust format string reference

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Uses


format!("Hello {}", "World!");          // Returns a formatted string from a fmtstr.
let mut vec = vec![""];
// Destination (first arg) must &impl mut io::Write or a &impl mut fmt::Write
write!(&mut vec, "Hello {}", "World!"); // Writes a fmtstr
writeln!(&mut vec, "Hello Me!");        // write! but appends a newline
print!("Hello {}", "World!");           // Prints a fmtstr to stdout
println!("Hello {}", "World!");         // print! but appends a newline
eprint!("Hello {}", "World!");          // Prints a fmtstr to stderr
eprintln!("Hello {}", "World!");        // eprint! but appends a newline
format_args!("Hello {}", "World!");     // Returns fmt::Arguments, see https://doc.rust-lang.org/std/fmt/#format_args
 

Specifying Arguments

All format macros start with a format string, with the exception of
write!
and
writeln!
requiring a additional destin­ation argument. It can contain a number of formats specified below. Positional arguments are specified immedi­ately after, and then named arguments in the
ident = val
format.

Refere­ncing Arguments

{}
Next positional argument1
{n}
n-th positional argument, starting from 0
{ident}
"­ide­nt" named argument, if nonexi­stent "­ide­nt" variable from the scope
{{
Literal
{
}}
Literal
}
1 Ex. The second usage of
{}
would be the second positional argument.