It prints text to the console followed by a new line, allowing you to display output to the user.
Click to reveal answer
beginner
How do you print a variable's value using println!?
Use curly braces {} as a placeholder inside the string, and pass the variable after the comma. Example: println!("Value: {}", x);
Click to reveal answer
beginner
What happens if you use println! without any arguments?
It prints an empty line (just a new line) to the console.
Click to reveal answer
intermediate
Can println! print multiple variables? How?
Yes, by using multiple placeholders {} and passing variables in order. Example: println!("{} and {}", a, b);
Click to reveal answer
intermediate
What is the difference between print! and println! macros?
print! prints text without adding a new line at the end, while println! adds a new line after printing.
Click to reveal answer
What will println!("Hello, {}!", "Rust"); output?
AHello, Rust!
BHello, {}!
CHello, "Rust"!
DError
✗ Incorrect
The placeholder {} is replaced by the string "Rust", so it prints: Hello, Rust!
Which macro adds a new line after printing?
Aprint!
Bprintln!
Cwrite!
Dformat!
✗ Incorrect
println! adds a new line after printing, unlike print!.
How do you print two variables a and b using println!?
Aprintln!("{} {}", a, b);
Bprintln!(a, b);
Cprintln!("a and b");
Dprintln!("{}", a + b);
✗ Incorrect
Use placeholders for each variable and pass them in order.
What does println!(); print?
ANothing
BA blank line
CError
DThe text 'println!'
✗ Incorrect
It prints a blank line (just a new line).
Which of these is correct to print the value of variable x?
Aprintln!("x");
Bprintln!("{}", x);
Cprintln!(x);
Dprintln!("{x}");
✗ Incorrect
Use {} as a placeholder and pass the variable after the comma.
Explain how to use the println! macro to print text and variables in Rust.
Think about how you combine text and values inside the macro.
You got /4 concepts.
What is the difference between print! and println! macros? When would you use each?
Consider how output appears on the console.
You got /4 concepts.
Practice
(1/5)
1. What does the println! macro do in Rust?
easy
A. Creates a new variable
B. Prints text or values to the screen with a new line
C. Reads input from the user
D. Stops the program execution
Solution
Step 1: Understand the purpose of println!
The println! macro is used to show messages or values on the screen, adding a new line after printing.
Step 2: Compare with other options
Reading input, creating variables, or stopping the program are not tasks of println!.
Final Answer:
Prints text or values to the screen with a new line -> Option B
Quick Check:
println! prints output [OK]
Hint: Remember: println! always prints with a new line [OK]
Common Mistakes:
Confusing println! with input functions
Thinking println! creates variables
Assuming println! stops the program
2. Which of the following is the correct syntax to print the number 5 using println! in Rust?
easy
A. println!("{}", 5);
B. println!(5);
C. println!("number: 5");
D. println!("5");
Solution
Step 1: Understand how to print values with placeholders
To print a number using println! with a placeholder, use {} inside the string and pass the value after the comma.
Step 2: Check each option
println!("5"); prints the string "5" literally, println!(5); is invalid syntax, println!("number: 5"); prints a fixed string without using a placeholder. println!("{}", 5); correctly uses {} to print the number 5.
Final Answer:
println!("{}", 5); -> Option A
Quick Check:
Use curly braces {} for values [OK]
Hint: Use curly braces {} to insert values inside println! [OK]
Common Mistakes:
Trying to print numbers without quotes or placeholders
Forgetting the comma between string and value
Using invalid syntax without quotes
3. What will be the output of this Rust code?
let x = 10;
println!("Value: {}", x);
medium
A. Value: 10
B. Value: x
C. Value: {}
D. 10
Solution
Step 1: Identify the variable and placeholder
The variable x holds the value 10. The println! uses {} to insert the value of x.
Step 2: Understand the output format
The output will replace {} with 10, printing "Value: 10" exactly.
Final Answer:
Value: 10 -> Option A
Quick Check:
Placeholder {} shows variable value [OK]
Hint: Curly braces {} show variable values in output [OK]
Common Mistakes:
Printing the variable name instead of its value
Printing the placeholder literally
Omitting the variable in println!
4. Identify the error in this Rust code:
let name = "Alice";
println!("Hello, name!");
medium
A. String quotes are incorrect
B. Variable name is not declared
C. println! macro is misspelled
D. Missing curly braces to print variable value
Solution
Step 1: Check how the variable is used in println!
The code prints the string "Hello, name!" literally because it lacks curly braces to insert the variable name.
Step 2: Identify the fix
To print the value of name, use {} inside the string and pass name after the comma.
Final Answer:
Missing curly braces to print variable value -> Option D
Quick Check:
Use {} to insert variables in println! [OK]
Hint: Use {} and comma to print variables, not plain text [OK]
Common Mistakes:
Forgetting curly braces around variable
Not passing variable after the string
Assuming variable prints without placeholders
5. You want to print the message: "Temperature is 23°C" using println!. Which code correctly prints this message including the degree symbol?
hard
A. println!("Temperature is 23C");
B. println!("Temperature is 23\degree C");
C. println!("Temperature is 23\u{00B0}C");
D. println!("Temperature is 23\xB0C");
Solution
Step 1: Understand how to print special Unicode characters
The degree symbol (°) can be printed using Unicode escape \u{00B0} in Rust strings.
Step 2: Check each option for correctness
println!("Temperature is 23\u{00B0}C"); uses the correct Unicode escape. println!("Temperature is 23C"); does not include the degree symbol. println!("Temperature is 23\degree C"); uses an invalid escape. println!("Temperature is 23\xB0C"); uses a hex escape which is incorrect for Unicode in Rust strings.
Final Answer:
println!("Temperature is 23\u{00B0}C"); -> Option C
Quick Check:
Use \u{XXXX} for Unicode chars in Rust strings [OK]
Hint: Use \u{XXXX} for special Unicode characters in strings [OK]
Common Mistakes:
Using invalid escape sequences
Assuming special characters print without escapes
Confusing hex and Unicode escapes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.