Getting Started with Rust
Rust is a systems programming language focused on three goals: safety, speed, and concurrency. It accomplishes these goals without a garbage collector, making it useful for a number of use cases other languages aren't good at: embedding in other languages, programs with specific space and time requirements, and writing low-level code, like device drivers and operating systems.
Rust improves on current languages targeting this space by having a number of compile-time safety checks with no runtime overhead, while eliminating all data races. Rust also aims to achieve zero-cost abstractions even though some of these abstractions feel like those of a high-level language. Even then, Rust still allows the fine-grained control that a low-level language would give.
Installation
The first step to using Rust is to install it. Generally speaking, you'll need an internet connection to run the commands in this chapter, as we'll be downloading Rust from the internet.
We'll be showing off a number of commands using a terminal, and those lines all start with
$. You don't need to type in the $ characters; they indicate the start of
each command. You'll see many tutorials and examples around the web that follow this convention:
$ for commands run as a regular user, and # for commands you should be
running as an administrator.
Installing on Linux or Mac
If you're on Linux or a Mac, all you need to do is open a terminal and type this:
$ curl https://sh.rustup.rs -sSf | sh
This will download a script, and start the installation. You may be prompted for your password. If it all goes well, you'll see this appear:
Rust is installed now. Great!
Installing on Windows
On Windows, go to rustup.rs and follow the instructions to download rustup-init.exe. Run that and follow the instructions it gives you.
The rest of the Windows-specific commands in the book will assume that you are using
cmd as your shell. If you use a different shell, you may be able to run the same commands
that Linux and Mac users do.
Hello, World!
Now that you have Rust installed, let's write your first Rust program. It's traditional when learning a new language to write a little program that prints the text "Hello, world!" to the screen, and in this section, we'll follow that tradition.
The nice thing about starting with such a simple example is that you can quickly verify that your compiler is installed, and that it's working properly. Printing information to the screen is also just a pretty common thing to do, so practicing it early is good.
Creating a Project File
First, make a file to put your Rust code in. Rust files always end in a .rs extension. If you're using more than one word in your filename, use an underscore to separate them; for example, use hello_world.rs rather than helloworld.rs.
Now open the hello_world.rs file you just created, and type the following code:
fn main() {
println!("Hello, world!");
}
Save the file, and go back to your terminal window. On Linux or OSX, enter the following commands:
$ rustc hello_world.rs
$ ./hello_world
Hello, world!
Key Features
- Memory safety — Rust's ownership system guarantees memory safety at compile time
- Zero-cost abstractions — High-level constructs compile down to efficient machine code
- Fearless concurrency — The type system prevents data races at compile time
- No garbage collector — Predictable performance without GC pauses
- Interoperability — Easy FFI with C and other languages
Rust has a number of features that are unique to it, and some that it shares with other languages. Let's have a look at what makes Rust special, and how it achieves its goals of safety, speed, and concurrency.
Learning More
The best way to learn Rust is to read The Rust Programming Language, also known as "the book". It covers everything from basic syntax to advanced features like lifetimes and unsafe code.
You can also find help in the Rust community forums, the Rust Discord server, and on Stack Overflow.