Your First Rust Program: 'Hello, World!' (Part 1)
After setting up and configuring your development environment, you are now ready to write your first Rust program. The "Hello, World!" program is a tradition in computer programming. It's a simple program that outputs "Hello, World!" to the screen. This tradition is a great way to make sure that your environment is set up correctly and to get a feel for a new language.
📚 Prerequisites
Before we begin, you must have:
- The Rust toolchain installed (via
rustup). - A terminal or command prompt.
🎯 Article Outline: What You'll Master
In this article, you will learn how to:
- ✅ Create a new Rust project using Cargo.
- ✅ Understand the structure of a Cargo project.
- ✅ Run your first Rust program.
🧠 Section 1: Creating a New Project with Cargo
The easiest way to start a new Rust project is by using Cargo. As we learned in a previous article, Cargo is Rust's build system and package manager.
To create a new project, open your terminal and run the following command:
cargo new hello_world
This command does two things:
- It creates a new directory called
hello_world. - It generates a new Rust project inside that directory with a "Hello, World!" program already written for you.
You should see the following output in your terminal:
Created binary (application) `hello_world` package
💻 Section 2: Exploring the Project Structure
Now, let's look at what Cargo has created for us. Navigate into the new directory:
cd hello_world
If you list the files in this directory, you will see a src directory and a Cargo.toml file.
hello_world/
├── Cargo.toml
└── src/
└── main.rs
Cargo.toml: This is the manifest file for your project. It contains metadata like the project's name, version, and dependencies.src/main.rs: This is the source code for your program. All your Rust code will go in thesrcdirectory.
Let's look at the contents of src/main.rs:
// src/main.rs
fn main() {
println!("Hello, world!");
}
This is the entire program. We will dissect this code in the next article. For now, let's just run it.
🚀 Section 3: Running Your First Rust Program
One of the best features of Cargo is that it can build and run your program with a single command. From inside your hello_world directory, run the following command in your terminal:
cargo run
You will see some output from Cargo as it compiles your program, followed by the output of the program itself:
Compiling hello_world v0.1.0 (/path/to/your/hello_world)
Finished dev [unoptimized + debuginfo] target(s) in 0.50s
Running `target/debug/hello_world`
Hello, world!
Let's break down what just happened:
Compiling: Cargo invoked the Rust compiler (rustc) to compile yourmain.rsfile into an executable.Finished: The compilation was successful. Cargo created a "development" build, which is optimized for debugging.Running: Cargo then ran the compiled executable, which is located attarget/debug/hello_world.Hello, world!: This is the output from your program.
Congratulations! You have successfully written and run your first Rust program.
✨ Conclusion & Key Takeaways
You've taken your first step into the world of Rust programming. You've learned how to use Cargo to create, build, and run a new project. This simple workflow is the foundation for all your future Rust development.
Let's summarize the key takeaways:
cargo newis the command to create a new Rust project.- Cargo creates a standard project structure with a
srcdirectory for your code and aCargo.tomlfile for configuration. cargo runis the command to compile and run your project.
Challenge Yourself:
Modify the src/main.rs file to print a different message. For example, change "Hello, world!" to "Hello, Rust!". Run cargo run again. Did you see your new message?
➡️ Next Steps
You've seen the "how" of running a Rust program. In the next article, "Your First Rust Program: 'Hello, World!' (Part 2)", we will dive into the "why" by dissecting the main.rs file line by line to understand the basic structure of a Rust program.
You've started the engine. Now it's time to learn how it works.
Glossary (Rust Terms)
- Binary (application): A type of crate that is an executable program, as opposed to a library, which is meant to be used by other programs.