Lesson 1 of 22
Lesson Progress: 0%
Hello World is the first program most people write when learning a new programming language. In Rust, the println! macro is used to display text on the screen. The fn main()function is the entry point of every Rust program — it is where execution begins. Writing a Hello World program introduces you to the basic structure of a Rust project: defining a function, using a macro, and producing output. This simple exercise confirms that your Rust environment is set up correctly and that you understand the fundamental syntax. From here, you can build more complex programs by adding variables, functions, and control flow.
fn main() {
println!("Hello, world!");
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
fn main() declares the main function, which is the starting point of every Rust program.{ and }.println! is a Rust macro that prints text followed by a newline."Hello, world!" is a string literal enclosed in double quotes.;.fn main() {
let name = "Rust";
println!("Hello, {}!", name);
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
let creates a new variable. Here name holds the string "Rust".name as &str (a string slice) automatically.{} inside the string act as placeholders for values.println!("Hello, !", name); replaces with the value of name.fn main() {
println!("Hello, world!");
println!("Welcome to Rust.");
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
println! calls, each producing one line of output.main.println! prints Hello, world! and the second prints Welcome to Rust..println! automatically adds a newline at the end of its output.fn greet() {
println!("Hello, world!");
}
fn main() {
greet();
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
fn followed by the function name and parentheses.fn greet() defines a function named greet that prints a message.main calls greet() by writing its name followed by parentheses.greet() is called, execution jumps to that function, runs its code, and returns.fn main() {
let x = 42;
println!("The answer is {}", x);
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
x holds the integer 42.x as type i32 (a 32-bit integer) automatically.{} works for numbers just like it does for strings.println!("The answer is ", x); inserts the value 42 into the printed text.fn main() {
for i in 0..3 {
println!("Hello {}", i);
}
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
for loop repeats a block of code a specific number of times.0..3 creates a range that produces the numbers 0, 1, and 2.i takes each value in the range, one at a time.println!("Hello ", i); prints Hello 0, then Hello 1, then Hello 2.Test Incomplete