Rust printing writes text or formatted values to an output stream. The basic tools are the print! and println! macros: print! leaves output on the current line, while println! adds a newline. section 1.0 section 2.10
Begin with a small string, then add formatting when the output needs values, alignment, escapes, or another representation. Rust’s compiler checks the formatting choices before the program runs. section 1.0 section 2.10
Printing can handle several common output tasks:
- Insert values with positional arguments such as {0}, named arguments such as {city1}, or ordinary placeholders such as {}. section 2.10
- Represent values with Display, Debug, pretty Debug, binary, hexadecimal, or octal formatting. section 2.10
- Control layout with padding, alignment, minimum widths, maximum widths, and fill characters. section 2.10
- Print multiline text, escaped characters, raw strings, ASCII bytes, Unicode characters, and pointer addresses. section 2.10
Formatting a small report
fn main() { let title = "TODAY'S NEWS"; println!("{:-^30}", title); let a = "SEOUL"; let b = "TOKYO"; println!("{city1:-<15}{city2:->15}", city1 = a, city2 = b); } The first line centers the title with hyphens; the second aligns two named values in fixed-width fields. section 2.10
fn main() { #1
print!("\t Start with a tab\nand move to a new line");
}
Compare the compact print! example with its visible tab and newline output to see how escape sequences affect layout. section 2.10




