๐ Scenario: You are learning how to write a simple Rust program. Rust programs start with a main function where the code runs. You will create a basic program that prints a greeting message.
๐ฏ Goal: Build a Rust program with a main function that prints Hello, Rust! to the screen.
๐ What You'll Learn
Create a main function
Use println! macro to print text
Print exactly Hello, Rust!
๐ก Why This Matters
๐ Real World
Every Rust program starts with a <code>main</code> function. This structure is the foundation for building command-line tools, servers, and more.
๐ผ Career
Understanding program structure is essential for any Rust developer. It helps you organize code and build applications that run correctly.
Progress0 / 4 steps
1
Create the main function
Write a fn main() function with empty curly braces {} to start your Rust program.
Rust
Hint
The main function is the entry point of a Rust program. It always starts with fn main() followed by curly braces.
2
Add a print statement inside main
Inside the main function, write println!("Hello, Rust!"); to print the greeting message.
Rust
Hint
The println! macro prints text to the screen. Remember to include the exclamation mark and the semicolon.
3
Check the program structure
Make sure your program has the fn main() function and inside it the line println!("Hello, Rust!");.
Rust
Hint
Double-check that your print statement is inside the main function and spelled exactly.
4
Run and display the output
Run the program and print the output. Write println!("Hello, Rust!"); inside main and then run it to see the message.
Rust
Hint
When you run the program, the console should show Hello, Rust!.
Practice
(1/5)
1. In a Rust program, where does the execution start?
easy
A. At the last line of the file
B. Inside the main function
C. Inside a function named start
D. At the first line of the file outside any function
Solution
Step 1: Understand Rust program entry point
Rust programs always begin execution from the main function.
Step 2: Check other options
Code outside functions or other function names like start are not entry points.
Final Answer:
Inside the main function -> Option B
Quick Check:
Rust starts at main() [OK]
Hint: Rust always runs code starting from main() [OK]
Common Mistakes:
Thinking code outside main() runs first
Assuming a function named start() runs automatically
Believing execution starts at the last line
2. Which of the following is the correct way to write the main function in Rust?
easy
A. function main() { println!("Hello, world!"); }
B. main() { println!("Hello, world!"); }
C. fn main() { println!("Hello, world!"); }
D. def main() { println!("Hello, world!"); }
Solution
Step 1: Identify Rust function syntax
Rust functions start with fn, followed by the name and parentheses.
Step 2: Check each option
Only fn main() { println!("Hello, world!"); } uses fn main() correctly; others use wrong keywords.
Final Answer:
fn main() { println!("Hello, world!"); } -> Option C
The main function starts with fn main() { but never closes with a }.
Step 2: Verify other syntax
The semicolon after println! is present, comment syntax is correct, and println! is spelled correctly.
Final Answer:
Missing closing brace '}' for main function -> Option D
Quick Check:
Every { needs a matching } [OK]
Hint: Count opening and closing braces carefully [OK]
Common Mistakes:
Forgetting to close function with }
Confusing comment syntax with errors
Missing semicolon errors instead of braces
5. You want to add a comment explaining why your Rust program prints "Hello". Which is the correct way to add a single-line comment inside the main function?
hard
A. // This prints Hello
println!("Hello");
B. # This prints Hello
println!("Hello");
C. /* This prints Hello */ println!("Hello");
D. println!("Hello");
Solution
Step 1: Identify Rust comment syntax
Rust uses // for single-line comments and /* */ for block comments.
Step 2: Check each option
Only // This prints Hello
println!("Hello"); uses Rust's single-line comment syntax correctly; /* ... */ is a block comment, # is Python-style, and <!-- ... --> is HTML.
Final Answer:
// This prints Hello
println!("Hello"); -> Option A
Quick Check:
Single-line comments start with // [OK]
Hint: Use // for single-line comments in Rust [OK]
Common Mistakes:
Using # or for comments
Confusing block comments with single-line
Placing comments inside println! macro
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.