Bird
Raised Fist0
Rustprogramming~5 mins

Output using println macro in Rust - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Output using println macro
Start Program
↓
Call println! macro
↓
Format string and arguments
↓
Send output to console
↓
Continue or End Program
The program starts, calls the println! macro which formats the string and arguments, then sends the output to the console before continuing or ending.
Execution Sample
Rust
fn main() {
    println!("Hello, world!");
}
This code prints the text 'Hello, world!' to the console.
Execution Table
StepActionEvaluationOutput
1Enter main functionStart program
2Call println! macroFormat string "Hello, world!"
3Print output to consoleOutput sentHello, world!
4End main functionProgram ends
💡 Program ends after printing the message.
Variable Tracker
VariableStartAfter println! callFinal
No variables---
Key Moments - 2 Insights
Why does println! print the message immediately?
Because println! is a macro that formats the string and sends it directly to the console output as shown in step 3 of the execution_table.
Is println! a function or something else?
println! is a macro, not a function. It expands at compile time to code that prints to the console, as seen in step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is printed at step 2?
A"Hello, world!"
BAn error message
CNothing
DThe program name
💡 Hint
Check the Output column at step 2 in the execution_table.
At which step does the program end according to the execution_table?
AStep 4
BStep 2
CStep 3
DStep 1
💡 Hint
Look at the Action and Evaluation columns to find when the program ends.
If we change the string inside println! to "Hi!", what changes in the execution_table output?
AProgram will not compile
BOutput at step 3 changes to "Hi!"
CNo change in output
DOutput at step 4 changes
💡 Hint
Focus on the Output column at step 3 where the printed message appears.
Concept Snapshot
println! macro prints text to the console.
Syntax: println!("text");
It formats the string and outputs immediately.
It is a macro, not a function.
Used for simple console output in Rust.
Full Transcript
This visual execution shows how the println! macro works in Rust. The program starts and enters the main function. Then it calls println! with the string "Hello, world!". The macro formats the string and sends it to the console output immediately. The output "Hello, world!" appears on the screen. Finally, the program ends. There are no variables involved in this simple example. Key points include understanding that println! is a macro and it prints immediately when called.

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

  1. 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.
  2. Step 2: Compare with other options

    Reading input, creating variables, or stopping the program are not tasks of println!.
  3. Final Answer:

    Prints text or values to the screen with a new line -> Option B
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    println!("{}", 5); -> Option A
  4. 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

  1. Step 1: Identify the variable and placeholder

    The variable x holds the value 10. The println! uses {} to insert the value of x.
  2. Step 2: Understand the output format

    The output will replace {} with 10, printing "Value: 10" exactly.
  3. Final Answer:

    Value: 10 -> Option A
  4. 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

  1. 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.
  2. Step 2: Identify the fix

    To print the value of name, use {} inside the string and pass name after the comma.
  3. Final Answer:

    Missing curly braces to print variable value -> Option D
  4. 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

  1. Step 1: Understand how to print special Unicode characters

    The degree symbol (°) can be printed using Unicode escape \u{00B0} in Rust strings.
  2. 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.
  3. Final Answer:

    println!("Temperature is 23\u{00B0}C"); -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard