๐ Scenario: You are curious about a programming language called Rust. You want to learn what it is and see a simple example of how to write and run a Rust program.
๐ฏ Goal: Learn what Rust is and write a simple Rust program that prints a message to the screen.
๐ What You'll Learn
Create a Rust program with a main function
Print a message explaining what Rust is
Use correct Rust syntax for printing
Understand the basic structure of a Rust program
๐ก Why This Matters
๐ Real World
Rust is used to build fast and reliable software like web browsers, games, and operating systems.
๐ผ Career
Knowing Rust can help you work on system-level programming jobs that require safety and performance.
Progress0 / 4 steps
1
Create the main function
Write a Rust fn main() function to start your program.
Rust
Hint
The main function is the entry point of a Rust program. It looks like fn main() { }.
2
Add a print statement
Inside the main function, add a println! macro to print the message "Rust is a systems programming language.".
Rust
Hint
Use println!("message"); to print text in Rust.
3
Explain Rust in a comment
Add a comment above the println! line explaining that Rust is safe and fast.
Rust
Hint
Comments start with // in Rust.
4
Run the program and show output
Run the Rust program and print the output it produces.
Rust
Hint
The program prints the message exactly as given.
Practice
(1/5)
1. What is Rust primarily known for?
easy
A. A fast and safe programming language
B. A type of database
C. A web browser
D. A mobile operating system
Solution
Step 1: Understand Rust's main features
Rust is designed to be fast and safe, focusing on preventing bugs.
Step 2: Compare options with Rust's purpose
Only A fast and safe programming language describes Rust as a fast and safe programming language.
Final Answer:
A fast and safe programming language -> Option A
Quick Check:
Rust = fast and safe language [OK]
Hint: Rust is a language, not software or hardware [OK]
Common Mistakes:
Confusing Rust with a database
Thinking Rust is a browser
Mixing Rust with operating systems
2. Which of the following is the correct way to declare a variable in Rust?
easy
A. int x = 5;
B. var x = 5;
C. let x = 5;
D. x := 5;
Solution
Step 1: Recall Rust variable declaration syntax
Rust uses the keyword let to declare variables.
Step 2: Check each option
Only let x = 5; uses let correctly; others use syntax from other languages.
Final Answer:
let x = 5; -> Option C
Quick Check:
Rust variables start with let [OK]
Hint: Rust uses 'let' to declare variables [OK]
Common Mistakes:
Using var instead of let
Using type declarations like int
Using := which is Go syntax
3. What will the following Rust code print?
fn main() {
let x = 10;
let y = 20;
println!("{}", x + y);
}
medium
A. 1020
B. Compilation error
C. x + y
D. 30
Solution
Step 1: Understand the code operations
Variables x and y hold 10 and 20. The code adds them: 10 + 20 = 30.
Step 2: Check the print statement
The println! macro prints the sum as a number, not concatenated strings.
Final Answer:
30 -> Option D
Quick Check:
10 + 20 = 30 [OK]
Hint: Add numbers inside println! to get output [OK]
Common Mistakes:
Thinking + concatenates strings
Expecting variable names printed
Assuming syntax error
4. Identify the error in this Rust code:
fn main() {
let mut x = 5;
x = "hello";
println!("{}", x);
}
medium
A. Cannot assign a string to an integer variable
B. Missing semicolon after println!
C. Variable x should not be mutable
D. println! macro syntax is incorrect
Solution
Step 1: Analyze variable type and assignment
Variable x is initially an integer (5). Later, it is assigned a string "hello" which is invalid.
Step 2: Check other syntax elements
Semicolons and println! syntax are correct; mutability is allowed but types must match.
Final Answer:
Cannot assign a string to an integer variable -> Option A
Quick Check:
Type mismatch error = Cannot assign a string to an integer variable [OK]
Hint: Variable types cannot change after declaration [OK]
Common Mistakes:
Ignoring type mismatch
Thinking mut allows any type
Assuming println! needs semicolon fix
5. You want to write a Rust program that safely handles user input and prevents bugs before running. Which feature of Rust helps achieve this?
hard
A. Garbage collection like Java
B. Ownership and borrowing system
C. Dynamic typing for flexibility
D. Automatic memory allocation without checks
Solution
Step 1: Understand Rust's safety features
Rust uses ownership and borrowing rules to manage memory safely at compile time.
Step 2: Compare with other options
Rust does not use garbage collection or dynamic typing; it enforces strict checks before running.
Final Answer:
Ownership and borrowing system -> Option B
Quick Check:
Rust safety = ownership and borrowing [OK]
Hint: Rust's safety comes from ownership rules [OK]
Common Mistakes:
Confusing Rust with garbage collected languages
Thinking Rust is dynamically typed
Ignoring ownership concept
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.