Hello World
1. Welcome and Icebreaker:
Welcome! I’m excited to guide you through your first steps in Rust.
To get started, let’s have a little icebreaker: Have you ever tried to learn a new programming language before, and if so, what was the most challenging part about it?
2. Outline the Session:
Here’s what we’ll do in this session:
- Concept Explanation: We will discuss the basic structure of a Rust program.
- Live Coding Demonstration: We’ll write the “Hello, World!” program together, step by step.
- Tasks: I will challenge you in task section
Learning Outcomes: By the end of this session, you will be able to:
- Understand the basic structure of a Rust program.
- Write, compile, and run a simple Rust program.
Expectations: I encourage you to ask questions at any point! The more interactive this is, the better you’ll learn.
3. Concept Explanation:
Every Rust program starts with a main function. This function is the entry point of your program – it’s where the execution begins. The code inside the main function’s curly braces {} is what gets executed.
In Rust, we use println! to print text to the console. The println! is a macro, which is a special kind of function that generates code. The exclamation mark ! indicates that it’s a macro and not a regular function. We put the text we want to print inside double quotes, and we pass that text as an argument to the println! macro.
4. Live Coding Demonstration:
Let’s write the code together. Open your favorite text editor or Rust IDE.
fn main() {
println!("Hello, World!");
}
Let’s break this down:
fn main(): This declares ourmainfunction. As mentioned, every Rust program needs one.{ ... }: The curly braces enclose the body of the function – the code that will be executed.println!("Hello, World!");: This line does the actual printing.println!: The macro for printing."Hello, World!": The text we want to print, enclosed in double quotes. This is a string literal.;: Almost every line in Rust ends with a semicolon. It tells the compiler that the statement is finished.
Saving and Running:
- Save: Save the file as
main.rs(or any name you like, but the.rsextension is important – it tells the system it’s a Rust file). - Open Terminal or CMD: Open your system in this directory.
- Compile: Use the below code to compile your code.
rustc main.rs
- Run
./main # On Linux and macOS
.\\main.exe # On Windows
You should see “Hello, World!” printed on your console!
5. Tasks
Modify the Greeting:
- Change the program to print “Hello, [Your Name]!” instead of “Hello, World!”. Make sure to replace
[Your Name]with your actual name. Then, recompile and run the modified program.