Day 2: Hello, Rust
Hello fellow Rustaceans 🦀!
Welcome back to another article in the "30 Days of Rust" series. Today, we're going to explore the anatomy of a Rust program. We'll learn how to compile a single-file and discuss strategies for managing larger projects with cargo. Let's dive into a simple Rust program and see how it transforms from plain text into an executable.
Let's create your very first Rust file! Start by making a file named main.rs
. Remember, all Rust files use the .rs
extension. Now, copy the code below and paste it into your shiny new main.rs
fn main() {
println!("Hello, Rust!");
}
Even though we're just starting our Rust journey, you can probably get a sense of what this program does. It looks like there's a function named main that's going to print something. And you're right! Let's break it down step by step:
We start with fn main()
. This is the heart of most Rust programs, the entry point where the action begins. Inside those curly braces, we find println!("Hello, Rust!");
. This line uses the println! macro to send the message "Hello, Rust!" to your screen, along with a friendly newline. Wait, did you catch that? Yes, println!
is a macro. Like C, Rust lets you use macros for metaprogramming. However, Rust's macros are way more powerful and much safer than their C counterparts.
Now, about that exclamation mark after println...
. Rust uses this to distinguish macros from regular functions. We'll explore macros in much more detail later!
Compiling our first program
Now, let's turn this code from words into action! Make sure you've saved your main.rs
file. Then, open up a terminal and navigate to the directory where you saved it.
- Type
rustc main.rs
and press Enter. This tells the Rust compiler (rustc) to do its thing. - If all goes well, you'll see a new file appear – likely named
main
ormain.exe
(depending on your operating system). This is your executable program! - To run it, type
./main
(or main.exe on Windows) and hit Enter. Ta-da! "Hello, Rust!" should greet you on the screen.
So, we know how to write and compile a single Rust file. That's awesome for small demos, but real-world projects get big – hundreds or even thousands of Rust files! Manually compiling everything would be a nightmare. This is where build systems come to the rescue.
The Classic (Hard) Way
We could do what we do with C projects:
- Read Compiler Manuals: Dig into the
rustc
documentation to understand how to compile and link multiple files. - Write a Makefile: Craft a Makefile to automate the process.
...But honestly, that sounds like a lot of work. Luckily, Rust has a much better solution!
Cargo: The Build Master
Since build systems are so important, the Rust folks created one tailored specifically for Rust – Cargo! It simplifies compiling, managing dependencies, packaging... basically, everything to do with building a project.
Let's ditch the manual approach and create a Rust project using Cargo:
Open a terminal and type cargo new hello
. This creates a new directory called hello
.
This is called a workspace. Let's peek inside and see what Cargo has created for us:
- Cargo.toml: This is the core of your project. It's where you define your project's name, version, dependencies, and more. Think of it like the Makefile for your Rust project.
- src/: Your Rust code lives here! You'll find a main.rs file, ready and waiting to be filled with your code.
- .gitignore: This file tells Git which files it shouldn't track. Wait, a
.gitignore
file? That means... yep! Cargo automatically sets up a Git repository when you create a workspace. You can customize the version control system if you like, but Git is the default. We'll delve into these features when we explore Cargo in more detail.
Let's get those gears turning! Paste our "Hello, Rust!" code into src/main.rs
.
fn main() {
println!("Hello, Rust!");
}
Now, navigate into the hello
directory and run cargo build. If everything goes smoothly, Cargo will compile your code. To run your project, use cargo run, and there it is – your friendly greeting!
Cargo doesn't just build your code, it also generates a couple of helpful things:
- Cargo.lock: This file keeps a record of the exact versions of your dependencies. Think of it as a way to make sure your project builds the same way every time.
- target/ directory: This is where all the compiled files and other build goodies end up.
Cargo has a lot more tricks up its sleeve, and we'll cover them as we continue our Rust adventure. For now, let's just appreciate how much easier it makes project setup and management!
Conclusion
Phew! We've got our first Rust project up and running! That wasn't so bad, was it? From here on out, we're going to dive deep into the core of Rust. Get ready, the fun really begins now! See you in the next article!🦀
Discussion