0
0
Rustprogramming~5 mins

Formatting output in Rust - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What macro is commonly used in Rust to print formatted output to the console?
The println! macro is used to print formatted output to the console in Rust.
Click to reveal answer
beginner
How do you insert a variable's value inside a string using Rust's formatting syntax?
Use curly braces {} as placeholders inside the string, and pass the variable after the comma. For example: println!("Value: {}", x);
Click to reveal answer
intermediate
What does the format specifier {:.2} do in Rust's formatting?
It formats a floating-point number to show exactly 2 digits after the decimal point.
Click to reveal answer
intermediate
How can you align text to the right with a width of 10 characters in Rust formatting?
Use {:>10} inside the format string. This pads the text on the left to make the total width 10 characters.
Click to reveal answer
beginner
What is the difference between print! and println! macros in Rust?
print! outputs text without adding a new line at the end, while println! adds a new line after printing.
Click to reveal answer
Which macro in Rust adds a newline after printing?
Aeprintln!
Bprint!
Cformat!
Dprintln!
How do you format a floating-point number to 3 decimal places in Rust?
A{:.3}
B{:3}
C{.3}
D{3}
What does {:>8} do in a format string?
ALeft-align text with width 8
BRight-align text with width 8
CCenter-align text with width 8
DTruncate text to 8 characters
Which macro returns a formatted string without printing it?
Aformat!
Bprint!
Ceprint!
Dprintln!
How do you include a literal curly brace character in a Rust format string?
AUse single braces {}
BEscape with backslash \{
CUse double braces {{ or }}
DUse parentheses ()
Explain how to format a floating-point number to 2 decimal places and align it right with width 10 in Rust.
Think about combining precision and alignment inside the curly braces.
You got /3 concepts.
    Describe the difference between print!, println!, and format! macros in Rust.
    Consider what each macro does with the output.
    You got /3 concepts.