Zero to One in Rust — Ep. 1: Let’s Get Rusty
As at the time of publishing, Rust has been voted as the most loved Programming Language on StackOverflow for 8 years in a row.
Rust is a promise of robust and efficient code. It’s a language designed for the modern world, addressing the ever-present challenges of software development. But, like any journey, it starts with a single step. Allow me to walk with you in this journey.
This episode will introduce you to Rust, why you should care about Rust as well as how to set up your environment and write your first “hello world”.
The aim is to be concise, and that’s exactly how we shall do this.
Why Rust?
In the rapidly evolving landscape of programming languages, Rust stands out for a multitude of reasons. Here are a few:
- Memory Safety: Rust was built from the ground up to provide robust memory safety. It practically eliminates null pointer dereferencing, data races, and other common sources of crashes and vulnerabilities. When you write Rust, you can trust your code to be safe.
- Performance: Rust’s zero-cost abstractions and fine-grained control over system resources make it a top choice for systems programming, embedded systems, and high-performance applications. You don’t have to choose between safety and performance; Rust offers both.
- Concurrency: Rust’s ownership and borrowing system allows for safe and efficient concurrency without the usual pitfalls of race conditions and data races. You can write concurrent code with confidence.
- Community and Ecosystem: The Rust community is vibrant and welcoming. You’ll find a rich ecosystem of libraries and tools, all backed by a community committed to open-source collaboration and quality.
Setting Up Your Environment
Install Rust
For *Nix users, it is recommended to install Rust usingrustup.
On your terminal, run:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
The current version of Rust as at the time of publishing is1.73.0
and that’s what we install. If you already installed Rust before, update it by running:
rustup update
For Windows users, please refer here.
Meet Cargo
The rustup
installation comes with cargo
, Rust’s build tool and package manager. For those coming in from the JS world, cargo
is like yarn
or npm.
In summary, we use cargo
to build our Rust code and dependencies. More on cargo
in the next episodes.
To ensure you have cargo
installed, run:
$ cargo --version
cargo 1.73.0 (9c4383fb5 2023-08-26)
Hello World
To ensure our toolchain is all tied together, we are going to write our first line of code: the hello world.
First, let’s make sure we have installed Rust properly:
$ rustc --version
rustc 1.73.0 (cc66ad468 2023-10-03)
Setup Project Structure
Let’s begin by setting up a directory to hold our code:
mkdir ~/zero-to-one-rust
cd ~/zero-to-one-rust
mkdir ep1
cd ep1
Inside the directory, let’s create our first Rust file, main.rs
touch main.rs
Inside it, let’s add our code:
fn main(){
println!("Hello, world");
}
Compiling & Running
To run the code, you have to first compile it.
$ rustc main.rs
$ ./main
Hello, world
Conclusion
To keep things short and sweet, let’s wrap up Episode 1 here. Over the next few episodes, we shall explore the anatomy of Rust functions, syntax and concepts in Rust.
Until next time, stay Rusty.
If we forget, we’ll waste memory. If we do it too early, we’ll have an invalid variable. If we do it twice, that’s a bug too — Steve Klabnik