Getting Started with Rust: A Beginner’s Guide
A beginner's guide to writing your first program with Rust and understanding key concepts such as macros, ownership and borrowing.
Published Oct 23, 2024
Last Modified Oct 25, 2024
Are you curious about Rust? Maybe you’ve heard about its memory safety or speed, and now you’re ready to try it for yourself. This guide will walk you through installing Rust, introduce some basic concepts, and help you write your first "Hello, World!" program in less than three minutes.
Rust’s official installer,
rustup
, makes getting started easy. Open your terminal and run:Follow the instructions on-screen, and once it’s done, you’ll have Rust installed. To confirm, type:
You should see the version number of Rust you’ve installed.
Now that you’ve installed Rust, let's create a simple "Hello, World!" program.
This is your first Rust program! The
fn
keyword defines a function, main
is the entry point of the program, and println!
well... that looks a bit funny. It doesn't look very function-like, but seems to work like a function... so what is that? Well, it's a macro!In Rust, macros are powerful tools that allow you to write code that writes other code. Macros differ from functions in that they operate on the abstract syntax tree of the code itself, which means they can perform transformations on the code during compilation. One of the most common macro is
println!
, which we just used in our program and, as expected, it prints output to the console. Macros in Rust are invoked with an exclamation mark, like println!
or vec!
.Macros can make your code more flexible and concise by abstracting repetitive patterns. They are a crucial part of the Rust ecosystem and allow developers to write highly optimized and reusable code. You'll encounter macros in many of Rust’s libraries, and you can even create your own to help automate common tasks in your projects.
To compile and run your Rust code, use the Rust compiler (
rustc
):You should see:
Hello, world!
That’s it! You’ve written and run your first Rust program. However, there is one very important thing that you should learn that differentiates Rust from the others and one the main reasons why Rust has been getting so much attention and growing so quickly: the ownership system!
You my have heard that Rust does not have a garbage collector. That in itself is uncommon, but Rust takes the concept further adopting a relatively unique approach to memory management. Languages like C, C++ or even old-school ones like Fortran and Assembly don't have a garbage collector either, but they put the full responsibility on the developer and just provide the means for you to do manage memory manually. Rust, on the other hand, finds the best of both to worlds through a unique system of ownership and borrowing to ensure memory safety.
Every value in Rust has a single owner, and when that owner goes out of scope, Rust automatically frees the memory.
While this is at play in every Rust program, this is hard to demonstrate with our simple "hello, world" program because the only piece of data that we have is the static string "Hello, World!" and this kind of data is allocated at compile time. Rust puts all string literals in a fixed location in memory for the whole duration of the program and make them immutable so this is all handled at compile time.
Take a more complex code snippet such as this one:
Here, we create a
String
using String::from
. The String
type is heap-allocated, meaning it takes up memory that needs to be managed explicitly (unlike the string literal "hello, world!" we used earlier, which is stored in the binary itself).s1
becomes the owner of this string, meaning it is responsible for cleaning up the memory when it goes out of scope.We then do something very interesting in the next line. At first glance, especially if you're familiar with other programming languages, line 3 may seem like a simple assignment. You may expect s1 and s2 to just hold the same value from this line now, but think again!
When we write
let s2 = s1
what we are telling Rust is that we now want s2 to own the memory that s1 had ownership of. So this is a handover not an assignment. If you try to use s1 from that point on it will fail because s1 has gone out of scope and you'll get a compile-time error. If your intention is to have both of s1 and s2 hold the same value you need to do a deep copy instead. You can do that by using clone():
You can use immutable and mutable references. So, for example, if you wanted s2 to point to the same reference as s1 you can assign it a reference to s1 instead like this:
This will allow s2 to use the data stored in s1 but not change it since it is an immutable reference. If you wanted to allow both s1 and s2 to change the data they point to then you could assign s2 an mutable reference like this:
As you can see the syntax starts becoming a bit more involved. Either way, especially if you've used C or C++ before and experienced pointer hell, you may be asking yourself how this plays with the ownership system since pointer references are one of the main causes of bad memory management. Well, unlike those other languages, Rust makes sure at compile time that references do not outlive owners. You will get an error if, for example, you declared s2 outside that code block like this:
Ownership is key to write fast programs that are safe from common errors like segmentation faults where your program attempts to access memory that it is not allowed to access. This system is the cornerstone of Rust’s design, and as you dive deeper, you'll learn to appreciate the simplicity and power of this unique system.
Now that you’ve written your first Rust program and hopefully got a good grasp on the concept of ownership and what makes Rust unique, I recommend you go build an app to really solidify and start going deeper.
I recommend you try building a calculator program, always a great follow-up to a hello, world program. I also recommend you using a coding assistant like Amazon Q which you can use to learn while doing and helping you understand concepts as you go and troubleshoot and explain things when they go wrong!
Follow me everywhere as @codingmatheus (linkedin/tiktok/youtube/instagram) for more tech content and tutorials and let me know in the comments if you have any questions or would like to see content about something else.
Happy coding! 🤗