Bird
Raised Fist0
Rustprogramming~20 mins

Formatting output in Rust - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Rust Formatting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
What is the output of this Rust code using format! macro?

Consider the following Rust code snippet that formats a floating-point number:

let pi = 3.14159;
let formatted = format!("{:.2}", pi);
println!("{}", formatted);

What will be printed?

Rust
let pi = 3.14159;
let formatted = format!("{:.2}", pi);
println!("{}", formatted);
A3.14
B3.14159
C3.1
D3
Attempts:
2 left
๐Ÿ’ก Hint

Look at the format specifier {:.2}. It controls the number of decimal places.

โ“ Predict Output
intermediate
2:00remaining
What is the output of this Rust code using println! with padding?

What will this Rust code print?

let num = 42;
println!("{:>5}", num);
Rust
let num = 42;
println!("{:>5}", num);
A43
B 42
C41
D84
Attempts:
2 left
๐Ÿ’ก Hint

The {:>5} means right-align in a field of width 5.

๐Ÿ”ง Debug
advanced
2:00remaining
Why does this Rust code cause a compile error?

Examine this Rust code:

let name = "Alice";
println!("Hello, {:10}", name);

It causes a compile error. Why?

Rust
let name = "Alice";
println!("Hello, {:10}", name);
AThe format specifier {:10} is valid and code compiles without error.
BThe format specifier {:10} is missing alignment character, causing a compile error.
CThe format specifier {:10} is invalid for &str, causing a compile error.
DThe format specifier {:10} is valid, but the code is missing a semicolon.
Attempts:
2 left
๐Ÿ’ก Hint

Check if the format specifier works with strings in Rust.

โ“ Predict Output
advanced
2:00remaining
What is the output of this Rust code using named arguments in format string?

What will this Rust code print?

let width = 6;
let height = 4;
println!("Rectangle: {w} x {h}", w=width, h=height);
Rust
let width = 6;
let height = 4;
println!("Rectangle: {w} x {h}", w=width, h=height);
ARectangle: 4 x 6
BRectangle: {w} x {h}
CRectangle: 6 x 4
DRectangle: width x height
Attempts:
2 left
๐Ÿ’ก Hint

Look at how named arguments are passed to println!.

๐Ÿง  Conceptual
expert
3:00remaining
How does Rust's formatting traits affect output customization?

Rust uses traits like Display and Debug to control output formatting. Which statement best describes their difference?

A<code>Display</code> and <code>Debug</code> are interchangeable and produce the same output.
B<code>Display</code> formats output as JSON; <code>Debug</code> formats as XML.
C<code>Display</code> is only for numbers; <code>Debug</code> is only for strings.
D<code>Display</code> is for user-facing output; <code>Debug</code> is for developer debugging output.
Attempts:
2 left
๐Ÿ’ก Hint

Think about when you want pretty output for users vs detailed info for debugging.

Practice

(1/5)
1. What does the placeholder {} do in Rust's println! macro?
easy
A. It comments out the line in the code.
B. It inserts the value of a variable or expression into the string.
C. It creates a new variable inside the string.
D. It repeats the string multiple times.

Solution

  1. Step 1: Understand the role of {} in formatting

    The {} acts as a placeholder where Rust inserts the value of a variable or expression inside a string.
  2. Step 2: Recognize its use in println!

    When using println!("Hello, {}", name);, Rust replaces {} with the value of name.
  3. Final Answer:

    It inserts the value of a variable or expression into the string. -> Option B
  4. Quick Check:

    Placeholder {} = Insert value [OK]
Hint: Remember: {} means insert value here [OK]
Common Mistakes:
  • Thinking {} creates variables
  • Assuming {} repeats strings
  • Confusing {} with comments
2. Which of the following is the correct syntax to format a floating-point number with 2 decimal places in Rust?
easy
A. println!("{:.2}", 3.14159);
B. println!("{2f}", 3.14159);
C. println!("{.2f}", 3.14159);
D. println!("{2}", 3.14159);

Solution

  1. Step 1: Identify correct decimal formatting syntax

    Rust uses {:.2} inside println! to format a float with 2 decimal places.
  2. Step 2: Check each option

    println!("{:.2}", 3.14159); uses {:.2}, which is correct. Others use invalid or incorrect syntax.
  3. Final Answer:

    println!("{:.2}", 3.14159); -> Option A
  4. Quick Check:

    Decimal places = {:.2} [OK]
Hint: Use {:.N} to set N decimal places [OK]
Common Mistakes:
  • Using {2f} or {.2f} which is invalid in Rust
  • Omitting the colon before precision
  • Confusing with other language formats
3. What is the output of this Rust code?
let x = 255;
println!("Decimal: {}, Hex: {:x}, Binary: {:b}", x, x, x);
medium
A. Decimal: 255, Hex: FF, Binary: 11111111
B. Decimal: 255, Hex: ff, Binary: 1111111
C. Decimal: 255, Hex: 0xff, Binary: 11111111
D. Decimal: 255, Hex: ff, Binary: 11111111

Solution

  1. Step 1: Understand formatting specifiers

    {} prints decimal, {:x} prints lowercase hex, {:b} prints binary.
  2. Step 2: Convert 255 to hex and binary

    255 decimal is ff in hex (lowercase) and 11111111 in binary.
  3. Final Answer:

    Decimal: 255, Hex: ff, Binary: 11111111 -> Option D
  4. Quick Check:

    Hex lowercase = {:x}, Binary = {:b} [OK]
Hint: Use {:x} for hex lowercase, {:b} for binary [OK]
Common Mistakes:
  • Expecting uppercase hex without :X
  • Adding 0x prefix automatically
  • Miscounting binary digits
4. Identify the error in this Rust code snippet:
let num = 42;
println!("Number: {2}", num);
medium
A. The println! macro requires a semicolon inside the string.
B. The variable num is not declared correctly.
C. The placeholder {2} refers to an argument index that does not exist.
D. Rust does not support numbered placeholders.

Solution

  1. Step 1: Check placeholder indexing

    The placeholder {2} means use the third argument (index 2), but only one argument num is provided.
  2. Step 2: Understand argument count mismatch

    Since only one argument exists, index 2 is out of range, causing a runtime error.
  3. Final Answer:

    The placeholder {2} refers to an argument index that does not exist. -> Option C
  4. Quick Check:

    Placeholder index must match argument count [OK]
Hint: Count arguments before using numbered placeholders [OK]
Common Mistakes:
  • Using wrong index in placeholders
  • Assuming Rust auto-corrects missing arguments
  • Thinking semicolon needed inside string
5. You want to print a table of names and scores aligned in columns using Rust's formatting. Which code correctly aligns the names left in 10 spaces and scores right in 5 spaces?
let name = "Alice";
let score = 93;
// Which println! line is correct?
hard
A. println!("{:<10} | {:>5}", name, score);
B. println!("{:>10} | {:<5}", name, score);
C. println!("{:10} | {:5}", name, score);
D. println!("{:^10} | {:^5}", name, score);

Solution

  1. Step 1: Understand alignment specifiers

    {:<10} aligns left in 10 spaces, {:>5} aligns right in 5 spaces.
  2. Step 2: Match alignment to requirements

    Names should be left aligned, scores right aligned, so println!("{:<10} | {:>5}", name, score); matches perfectly.
  3. Final Answer:

    println!("{:<10} | {:>5}", name, score); -> Option A
  4. Quick Check:

    Left align = <, Right align = > [OK]
Hint: Use <: for left, >: for right alignment [OK]
Common Mistakes:
  • Swapping alignment symbols
  • Omitting width numbers
  • Using center alignment when not needed
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard