Welcome to your first look at the Rust programming language! This guide covers the basics of Rust, its modules, and its powerful package manager, Cargo. Rust is a systems programming language focused on safety, speed, and concurrency. Its strict compiler helps you write robust code by catching errors at compile time, not runtime. Let’s get your environment set up and build your first project.
Table of Contents
💻 Installing Rust and Your First Program
Getting Rust is simple. The recommended way is to use the `rustup.sh` script, which installs the compiler (`rustc`), the standard library, and the package manager (`cargo`).
- Run the installer:
curl -sSf https://static.rust-lang.org/rustup.sh | sh
- Create your ‘Hello World’ program: Save the following code in a file named
helloW.rs
.fn main() {
println!("Hello World!");
} - Compile and run:
rustc helloW.rs
./helloW
The println!
syntax is a Rust macro, which is a way of writing code that writes other code, providing powerful syntactic abstraction.
💻 Introducing Cargo: The Rust Package Manager
While you can compile single files with `rustc`, any serious project should use Cargo. Cargo handles building your code, downloading the libraries your code depends on (called crates), and building those libraries.
- Create a new project with Cargo:
cargo new hello_cargo --bin
- Examine the structure: Cargo creates a `hello_cargo` directory with a `Cargo.toml` file for metadata and dependencies, and a `src` directory containing `main.rs`.
- Build and run with Cargo: Navigate into the new directory and simply run
cargo run
. Cargo will compile and execute your program.
💻 Understanding Rust Modules
Rust’s standard library is organized into modules that provide a vast amount of functionality. These are accessed using the `std::` prefix. You don’t need to import them manually; they are available to all Rust crates by default. Some of the most important modules include:
std::env
: For interacting with the process’s environment.std::fs
: For cross-platform filesystem operations.std::net
: For TCP and UDP communication.std::thread
: For working with threads.
You’ll use these modules extensively as you build more complex applications in Rust.
More Topics
- Redis Guide: The High-Speed In-Memory Data Store
- Riak NoSQL Guide: What’s the Big Deal?
- MongoDB Guide: Build a Blog with Python and Bottle
- MongoDB Guide: An Admin’s Guide to Maximum Power
- MongoDB Guide: Using Native Drivers with Python and Ruby
- MariaDB Guide: The Open Source MySQL Alternative
- SQLite3 Guide: Getting Started with Python