Introduction to Rust Programming
Introduction to Rust
Why Rust?
Programming languages often force a trade-off. You can have speed and control, like you get with C++, but you risk memory bugs and crashes. Or you can have safety, like with Python or Java, but you give up some performance because of a background process called a garbage collector.
Rust offers a different deal. It’s a systems programming language that gives you the speed of C++ while also guaranteeing memory safety. It manages this without a garbage collector. Instead, it uses a unique system of rules, checked by the compiler, that ensures your code is safe before it even runs. This prevents entire categories of common bugs right from the start.
Rust is a systems programming language that focuses on safety, concurrency, and performance.
This focus on safety extends to concurrency, which is when different parts of a program execute independently. Writing concurrent code is notoriously difficult because of the risk of data races, where two threads access the same data at the same time. Rust's compiler helps prevent these issues, making it much easier to write fast, parallel programs that work correctly.
Setting Up Your Environment
Getting started with Rust is straightforward. The primary tool is rustup, a command-line tool for managing Rust versions and associated tools. Installing it also gives you cargo, Rust's build system and package manager. Cargo handles compiling your code, downloading the libraries your code depends on, and building those libraries.
To install Rust on your computer, open your terminal and run this command:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Follow the on-screen instructions. Once it's finished, you'll have the Rust compiler (rustc) and Cargo ready to go. You can verify the installation by opening a new terminal and running:
rustc --version
cargo --version
If you see version numbers for both, you're all set.
Your First Rust Program
Let's write a classic "Hello, world!" program. The easiest way to start a new project is with Cargo. Navigate to the directory where you want to keep your projects and run this command:
cargo new hello_world
Cargo creates a new directory called hello_world with a couple of files inside. The most important one is src/main.rs, which is where your application code lives. Open that file in your favorite text editor. It should look like this:
// This is the main function.
fn main() {
// Print text to the console.
println!("Hello, world!");
}
Let's break this down.
fn main()defines a function namedmain. Themainfunction is special; it's always the first code that runs in every executable Rust program.- The function body is wrapped in curly braces
{}. println!("Hello, world!");is the line that does the work.println!is a Rust macro that prints text to the screen. The!means you're calling a macro instead of a normal function. The line ends with a semicolon;, which indicates that this expression is over.
Rust's syntax might look familiar if you've seen C or C++. But its underlying rules are what make it unique.
To run the program, navigate into your new hello_world directory in your terminal. Then, use Cargo to compile and run it with a single command:
cd hello_world
cargo run
You should see the text Hello, world! printed to your terminal. Cargo first compiled your project and then ran the resulting executable. Congratulations, you've written and run your first Rust program!