ProgramGuru

Formatted output and comments

Formatted output and comments

A report should identify what a number means. Rust formatting can put values into a readable message rather than printing a sequence of unexplained numbers. Comments explain the reason for a rule; they do not execute and should not replace clear names.

What you’ll learn

  • Label values and control displayed decimal precision.
  • Distinguish diagnostic formatting from a stable data format.

Formatted output and comments in action

  1. Stored average: 77.5

    The first statement prints the average with its label.

    Stored average: 77.5
  2. Format to one decimal

    Debug formatting shows the array elements in brackets.

    Format to one decimal
  3. Labeled report line

    Escaped braces appear literally around score rather than looking for another value.

    Labeled report line
The first statement prints the average with its label. Debug formatting shows the array elements in brackets. Escaped braces appear literally around score rather than looking for another value.

Formatted output and comments explained

println! adds a newline while print! does not. Named captures use braces, and {:.1} displays one digit after the decimal point. {:?} requests Debug formatting for supported types. Literal braces are escaped as {{ and }} inside a format string.

Work through the Rust program

Save the complete source as main.rs in the Rust environment. No standard input is needed.

Rust 2024 · complete program

fn main() {
  // This report keeps one decimal place for the class average.
  let average = 77.5;
  println!("Average: {average:.1}");
  println!("Marks: {:?}", [75, 80]);
  println!("Rule: {{score}}");
}

Expected output

Average: 77.5
Marks: [75, 80]
Rule: {score}

Follow the execution

The first statement prints the average with its label. Debug formatting shows the array elements in brackets. Escaped braces appear literally around score rather than looking for another value.

Try the complete program in the Rust compiler. For tests, Cargo commands and local files, use the specified local environment.

Try a small variation

Your task: How would you display the average with two decimal places?

Rust 2024 · complete program

fn main() {
  // This report keeps one decimal place for the class average.
  let average = 77.5;
  println!("Average: {average:.1}");
  println!("Marks: {:?}", [75, 80]);
  println!("Rule: {{score}}");
}

Worked answer

Change the format precision to .2. The displayed value becomes 77.50; this changes presentation rather than changing the stored calculation.

Mistakes and limits to recognize

Debug output is useful for investigation but is not a stable data interchange format. Formatting precision does not make floating-point storage exact. For an interactive prompt without a newline, stdout may need an explicit flush.

Check your understanding

How would you display the average with two decimal places?
Choose one answer. You can try again.

Answer and explanation

Change the format precision to .2.

Change the format precision to .2. The displayed value becomes 77.50; this changes presentation rather than changing the stored calculation.

Take the idea into your next program

Change the format precision to .2. The displayed value becomes 77.50; this changes presentation rather than changing the stored calculation. Debug output is useful for investigation but is not a stable data interchange format.

Further reading: the official Rust Book on formatted output and comments.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Tuples and fixed-size arrays →