|
| 1 | +- # `format!` macro |
| 2 | + - The `format!` macro creates a formatted string by combining text with values |
| 3 | + - Part of Rust's standard library macro family for string formatting |
| 4 | + - ## Overview |
| 5 | + - Returns a `String` containing the formatted text |
| 6 | + - Uses the same formatting syntax as `println!` and `print!` macros |
| 7 | + - Evaluates at compile time where possible |
| 8 | + - ## Syntax |
| 9 | + - ```rust |
| 10 | + format!(format_string, args...) |
| 11 | + ``` |
| 12 | + - `format_string`: A string literal containing format specifiers |
| 13 | + - `args...`: Values to be formatted and inserted into the string |
| 14 | + - ## Format Specifiers |
| 15 | + - `{}` - Default formatting (uses `Display` trait) |
| 16 | + - `{:?}` - Debug formatting (uses `Debug` trait) |
| 17 | + - `{:#?}` - Pretty-printed debug formatting |
| 18 | + - `{0}` - Positional argument (0-indexed) |
| 19 | + - `{name}` - Named argument |
| 20 | + - `{:width}` - Minimum width |
| 21 | + - `{:0width}` - Zero-padded with minimum width |
| 22 | + - `{:.precision}` - Floating-point precision |
| 23 | + - `{:x}` - Hexadecimal formatting (lowercase) |
| 24 | + - `{:X}` - Hexadecimal formatting (uppercase) |
| 25 | + - `{:o}` - Octal formatting |
| 26 | + - `{:b}` - Binary formatting |
| 27 | + - ## Examples |
| 28 | + - ### Basic Usage |
| 29 | + - ```rust |
| 30 | + let name = "Alice"; |
| 31 | + let age = 30; |
| 32 | + let message = format!("Hello, {}! You are {} years old.", name, age); |
| 33 | + // message = "Hello, Alice! You are 30 years old." |
| 34 | + ``` |
| 35 | + - ### Positional Arguments |
| 36 | + - ```rust |
| 37 | + let s = format!("{1} and {0}", "second", "first"); |
| 38 | + // s = "first and second" |
| 39 | + ``` |
| 40 | + - ### Named Arguments |
| 41 | + - ```rust |
| 42 | + let s = format!("{name} is {age} years old", name = "Bob", age = 25); |
| 43 | + // s = "Bob is 25 years old" |
| 44 | + ``` |
| 45 | + - ### Debug Formatting |
| 46 | + - ```rust |
| 47 | + let vec = vec![1, 2, 3]; |
| 48 | + let s = format!("{:?}", vec); |
| 49 | + // s = "[1, 2, 3]" |
| 50 | + ``` |
| 51 | + - ### Number Formatting |
| 52 | + - ```rust |
| 53 | + let n = 42; |
| 54 | + format!("{:04}", n); // "0042" (zero-padded, width 4) |
| 55 | + format!("{:x}", n); // "2a" (hexadecimal) |
| 56 | + format!("{:X}", n); // "2A" (uppercase hex) |
| 57 | + format!("{:b}", n); // "101010" (binary) |
| 58 | + ``` |
| 59 | + - ### Floating-Point Precision |
| 60 | + - ```rust |
| 61 | + let pi = 3.14159265359; |
| 62 | + format!("{:.2}", pi); // "3.14" (2 decimal places) |
| 63 | + format!("{:.0}", pi); // "3" (no decimal places) |
| 64 | + ``` |
| 65 | + - ## Related Macros |
| 66 | + - [[Rust/println]] - Print formatted text to stdout with newline |
| 67 | + - [[Rust/print]] - Print formatted text to stdout |
| 68 | + - [[Rust/eprintln]] - Print formatted text to stderr with newline |
| 69 | + - [[Rust/eprint]] - Print formatted text to stderr |
| 70 | + - [[Rust/write]] - Write formatted text to a writer |
| 71 | + - [[Rust/writeln]] - Write formatted text to a writer with newline |
| 72 | + - ## Traits Used |
| 73 | + - `Display` - Default formatting trait (`{}`) |
| 74 | + - `Debug` - Debug formatting trait (`{:?}`) |
| 75 | + - ## Performance |
| 76 | + - Allocates a new `String` on the heap |
| 77 | + - For string concatenation without formatting, consider `String::from()` or `to_string()` methods |
| 78 | + - Formatting is evaluated at compile time when possible |
| 79 | + - ## Related |
| 80 | + - [[Rust/Macro]] - General macro concept in Rust |
| 81 | + - [[Rust/String]] - String type in Rust |
| 82 | + - [[Rust/std/fmt]] - Formatting module in standard library |
| 83 | + - [[Rust/Book/TRPL]] - The Rust Programming Language book |
0 commit comments