We use the println! macro to show messages or results on the screen. It helps us see what our program is doing.
Output using println macro in Rust
println!("text to show"); println!("Hello, {}!", name);
The println! macro prints text and adds a new line at the end.
You can insert values inside the text using curly braces {} and provide the values after the comma.
println!("Hello, world!");
let age = 25; println!("I am {} years old.", age);
let name = "Alice"; println!("Welcome, {}!", name);
This program prints a greeting with a name, shows a score, and then prints a congratulation message.
fn main() {
let name = "Bob";
let score = 42;
println!("Hello, {}!", name);
println!("Your score is {}.", score);
println!("Good job!");
}Remember to use the exclamation mark ! after println because it is a macro, not a function.
Each println! adds a new line automatically, so you don't need to add \n inside the string unless you want extra lines.
println! is used to print text and values to the screen with a new line.
You can insert values inside the text using curly braces {}.
It helps you communicate with the user or check what your program is doing.
Practice
println! macro do in Rust?Solution
Step 1: Understand the purpose of
Theprintln!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 ofprintln!.Final Answer:
Prints text or values to the screen with a new line -> Option BQuick Check:
println!prints output [OK]
- Confusing println! with input functions
- Thinking println! creates variables
- Assuming println! stops the program
println! in Rust?Solution
Step 1: Understand how to print values with placeholders
To print a number usingprintln!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 AQuick Check:
Use curly braces {} for values [OK]
- Trying to print numbers without quotes or placeholders
- Forgetting the comma between string and value
- Using invalid syntax without quotes
let x = 10;
println!("Value: {}", x);Solution
Step 1: Identify the variable and placeholder
The variablexholds the value 10. Theprintln!uses{}to insert the value ofx.Step 2: Understand the output format
The output will replace{}with 10, printing "Value: 10" exactly.Final Answer:
Value: 10 -> Option AQuick Check:
Placeholder {} shows variable value [OK]
- Printing the variable name instead of its value
- Printing the placeholder literally
- Omitting the variable in println!
let name = "Alice";
println!("Hello, name!");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 variablename.Step 2: Identify the fix
To print the value ofname, use{}inside the string and passnameafter the comma.Final Answer:
Missing curly braces to print variable value -> Option DQuick Check:
Use {} to insert variables in println! [OK]
- Forgetting curly braces around variable
- Not passing variable after the string
- Assuming variable prints without placeholders
"Temperature is 23°C" using println!. Which code correctly prints this message including the degree symbol?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 CQuick Check:
Use \u{XXXX} for Unicode chars in Rust strings [OK]
- Using invalid escape sequences
- Assuming special characters print without escapes
- Confusing hex and Unicode escapes
Ready to prove it?
Pass the test and this pattern moves unknown
