To write your first Rust program, use cargo new project_name, then open main.rs and add a main function with a println! macro. This prints a message to the terminal. Rust programs always start from the main function.
Once your Rust environment is set up, the next step is to write and understand your first program. This is usually called Hello, world!, and it is a simple example that prints a line of text. In this post, I will show you how to create it, run it, and explain what each part of the code means.
If you have not set up your environment yet, please read my earlier post: Setting Up the Rust Environment. It shows how to install Rust, create a new project, and run your code with cargo.
Creating the Hello World Project
To start, open your terminal and run:
cargo new hello_world
This creates a new folder named hello_world with everything you need:
cssCopyEdithello_world/
├── Cargo.toml
└── src/
└── main.rs
Move into the folder:
cd hello_world
Run the program:
cargo run
The output should be:
Hello, world!
Now let me explain what is happening in this project.
Understanding main.rs
Open the src/main.rs file. You will see:
fn main() {
println!("Hello, world!");
}
This is a complete Rust program. Let us break it down.
The fn Keyword
fn stands for “function.” Every Rust program starts from a function named main. This is where the execution begins.
fn main() {
}
There are no parameters here, and main does not return anything. In Rust, this is the standard entry point.
The println! Macro
Inside main, we use:
println!("Hello, world!");
This line prints text to the terminal.
println! is a macro, not a regular function. You can tell because of the exclamation mark. Rust uses macros to generate code at compile time. This one is used for writing to the terminal.
You can print anything you want by changing the text inside the double quotes. For example:
println!("Welcome to Rust programming!");
Rust always ends statements with a semicolon. That is why you see one at the end of the line.
Why Use cargo run?
When you use cargo run, it does two things:
- It compiles your code using rustc.
- It runs the compiled binary.
You can also compile without running:
cargo build
Then run the program manually:
./target/debug/hello_world
But for simple cases, cargo run is easier.
Changing the Output
Try modifying the text inside the quotes. Change:
println!("Hello, world!");
To:
println!("Rust is fun!");
Then run:
cargo run
You should see the new output.
This is how you begin experimenting. Try adding more println! lines to understand how the program flows.
Quick Exercise
Modify your program to print three different messages:
fn main() {
println!("This is my first Rust program.");
println!("I am learning step by step.");
println!("It feels clear and manageable.");
}
Then run it again and observe the output.
Summary
Rust programs always begin with a main function. Inside that, you use println! to show messages. The cargo new command sets up a full project, and cargo run compiles and runs it.
This program might be simple, but it teaches you the basic structure of all Rust applications. You now know how to start a project, understand where the program begins, and how to show output.
In the next post, I will walk through Variables and Mutability. This will help you learn how to store and change values in Rust, which works differently from JavaScript. If you have used let in JavaScript, you will see both similarities and important differences in Rust.
Next up: Variables and Mutability in Rust