Coding/Learn/rust/first program
Rust Foundational Course
01.Introduction to Rust & Memory Safety
02.Formatting, Comments & Macros (println!)
03.Variables, Mutability & Shadowing
04.Scalar & Compound Types (Integers, Floats, Tuples, Arrays)
05.Functions, Parameters & Return Expressions
06.Conditionals & If as an Expression
07.Ownership Rules & Move Semantics
08.References & Borrowing (&T)
09.Mutable References (&mut) & Aliasing XOR Mutability
10.Slices & String Types (&str vs String)
11.Loops in Rust (loop, while, for)
12.Pattern Matching with match Expressions
13.Structs, Associated Functions & Methods (impl)
14.Enums & the Option<T> Type (No Null!)
15.Concise Pattern Matching (if let & let else)
16.Vectors & Growable Collections (Vec<T>)
17.Error Handling with Result<T, E> & the ? Operator
18.Traits, Shared Behavior & Generics
19.Closures & Functional Iterators (map, filter, collect)
20.Capstone: High-Performance In-Memory Log Telemetry Engine
Course Certificate0%

Complete all lessons to earn

LearnFormatting, Comments & Macros (println!)
2 / 20 0%
Lesson 02 of 20
5 mins read

Formatting, Comments & Macros (println!)

PreviousNext Lesson
In Plain English

Rust macros like println! and format! use curly braces ({}) as placeholders for positional parameters, and {:?} for Debug printing. Semicolons denote statements in function bodies.

The Mental Model

"The {} placeholder in println! is like a customized name badge holder where you slot in the visitor’s card at print time."

Deep Dive: How It Works

Display ({}) vs Debug ({:?}): Display formats for end-user readability, while Debug prints internal structural state.

Semicolons: Semicolons turn expressions into statements. A line ending with a semicolon returns the unit type ().

Comments: Use // for line comments and /// for doc comments formatted in Markdown.

Syntax Blueprint

println!("Value: {}", val); | println!("Debug: {:?}", obj);

Use {} for Display types and {:?} for types implementing the Debug trait.

PointCore Rules to Remember

Point star
Positional Arguments: println!("{0} + {1} = {2}", a, b, c) formats by explicit index.
Point star
Named Arguments: println!("{user} is {status}", user="Alex", status="Active").
Point star
Debug Trait: {:?} prints structures, tuples, and arrays without custom string parsers.

Common Beginner Traps & How to Fix Them

Mistake:Trying to print an array or tuple with {}.

Why it happens: Complex compound structures do not implement the Display trait by default.

How to fix: Use the debug formatter {:?} or {:#?} (pretty-print) instead.

Pro Tip: Use dbg!(expression) during development—it prints file, line number, and value, then returns the value directly!

Live Interactive Example

Hit Run Code to see it live
Formatting and Debug Output
Rust 1.78 / rustc
1
2
3
4
5
6
7
8
9
Output Console
Click "Run Code" to view the rendered output.
How it works: Demonstrates standard {} string formatting and {:?} tuple debug printing.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Print formatted system telemetry

  • PointInside fn main(), declare let host = "node-01"; and let memory_mb = 512;
  • PointPrint "Host: node-01 | Memory: 512 MB" using println!.
1
2
3
4
5
6
7
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates