liveBook · Manning

Formatting

As explained in Learn Rust in a Month of Lunches

Formatting is the arrangement or transformation of content, data, or code according to specified structural, stylistic, or presentation conventions.

Rust formatting controls how text and values appear when printed. The `println!` macro supports escapes, raw strings, byte output, Unicode escapes, and formatted values. section 2.10

fn main() {
    let title = "TODAY'S NEWS";
    println!("{:-^30}", title);                                  #1
    let bar = "|";
    println!("{: <15}{: >15}", bar, bar);                        #2
    let a = "SEOUL";
    let b = "TOKYO";
    println!("{city1:-<15}{city2:->15}", city1 = a, city2 = b);  #3
}

Use fill characters, alignment markers, and field widths to shape output; named arguments can make the format string easier to follow. section 2.10

Other formatting options let you:

  • Choose how values appear, such as Debug output, pointer addresses, or binary, hexadecimal, and octal numbers. section 2.10
  • Select positional or named values and align them within a minimum width. section 2.10

How Learn Rust in a Month of Lunches covers it

How deep it goes
Demonstrates escapes, raw strings, byte output, Unicode escapes, pointers, numeric bases, argument selection, and alignment.
Examples
Uses file paths, hashtags, byte arrays, Unicode characters, pointer addresses, number bases, and aligned headings.
What it assumes
Builds on basic Rust printing and teaches string escapes and raw strings through examples.
What you can do after
Apply Rust println! formatting to print raw or escaped text, inspect bytes and addresses, select numeric bases, and align fields.
How it connects
Connects Rust formatting to string literals, Unicode characters, references, and numeric representations.

Also explained in

Compare how 7 books explain formatting →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Rust in a Month of Lunches. The section links point to the parts of the book it draws on.