- Rust Overview
- To Rust or Not To Rust
- When To Use Rust
- When Not To Use Rust
- Ownership / Memory in Rust
- Moves
- Borrowing
- Clone vs Copy
- Recommended Resources
- Community Links
- IDE Language Support
- Beginner Guides
- Other Guides
- References
- Opinions
- Tobias Hildebrandt...
Rust is a general-purpose programming language designed for safety and performance. It has been the "most loved language" on StackOverflow for several years in a row! Although primarily aimed towards a low-level, systems-programming audience, it can be also written at a high level without incurring runtime penalties. Programs written in Rust have certain guarantees provided at compile-time, including type- and memory-safety. These language features are not without drawbacks, such as increased language complexity and learning time. However, once learned, Rust can be written very efficiently and debugging time can be reduced greatly thanks to compile-time guarantees.
This guide is still a work-in-progress, so check out our beginner guides section for a more complete intro to the language. If you want to contribute, check it out on GitHub!
Rust Overview
To Rust or Not To Rust
When To Use Rust
-
You have enough time to learn the language
- Rust has a very welcoming and inclusive community! Beginners are encouraged to ask questions! See our community section below for links
- Correctness, run speed, and safety are more important to you than development speed
-
Your program needs low level access
- Though Rust is also great for high level programs :)
-
You are contributing to a project that already uses Rust
- While it is possible for Rust to interact with other languages (through
ffi
or Foreign Function Interface), most projects won't change their build system just for you!
- While it is possible for Rust to interact with other languages (through
-
Cross-platform development
- Rust programs can be compiled for Windows, macOS, Linux, Android, iOS, FreeBSD, and more
- Rust can be run in web browsers via WebAssembly (check out
wasm-pack
)
-
Using a system that doesn't have a standard library
- In Rust, you can disable the standard library with
no-std
- Often required when building operating system or programs for micro-controllers (check out AVR Rust
- In Rust, you can disable the standard library with
When Not To Use Rust
-
You don't want to spend the time to learn it
- But you should! That's what this guide is for :)
-
You need speedy development
- For events like hack-a-thons, game jams, code challenges
-
You don't want to or can't distribute binaries/executables
- Related: rust's standard library (
std
) depends onlibc
(C standard library) - There is an effort to re-implement necessary parts in rust, see here
- This also makes things more difficult for OSs like OpenBSD that don't have OS-level backwards compatibility
- Related: rust's standard library (
-
You want a short compile time
- Compilation in Rust, generally speaking, is slow, but it is incremental (don't have to recompile everything if you only change a single file)
-
Your employer/upper management is afraid of "immature" languages, see here for a similar opinion
- Rust technically does not have a formal language specification
rustc
is not self-hosted -- it requires bootstrapping- Rust has essentially only one complete compiler implementation (
rustc
), thoughmrustc
is promising - Rust still doesn't have great assembly support (though the
asm!
macro is almost there) - If you are using
nightly
(the unstable branch of rust), then some things in your code might break whenever rust is updated - Though this is less of an issue while using the
stable
branch
Ownership / Memory in Rust
What is ownership? From Chapter 4.1 of the Rust Book:
- Each value in Rust has a variable that’s called its owner.
- There can only be one owner at a time.
- When the owner goes out of scope, the value will be dropped.
Here is another decent primer on Rust memory.
Moves
You can change the owner of a value by "moving" the value. This is done by assigning it to a new variable. Note that if a resource implements Copy
, then it will not be moved during an assignment (the value will just be copied into the new variable). See Chapter 15.2 of Rust By Example.
let x = Vec::<i32>::new(); // create a new vector of 32 bit integers, x is the owner
let y = x; // y is now the owner of the vector, x is no longer the owner
// let z = x; // this would be an error, use after move!
Borrowing
To access a value without changing ownership, we can borrow the value by taking a reference to it. This is similar to pointers in C and C++. However, keep in mind that the Rust compiler guarantees that references always point to valid values, so your program won't even compile if you try to use an invalid reference. See Chapter 15.3 of Rust By Example for details.
let x = Vec::<i32>::new();
let y = &x; // y is reference to x
let z = &x; // z is also a reference to x
Clone vs Copy
In Rust, if you want to make a duplicate of some data, you can use the method .clone()
. Your data type must implement the trait Clone
. However, if your data type implements Copy
, then it will automatically be copied upon assignment.
let x = 10i32; // i32 implements Copy (as do all integers, floats, and chars)
let y = x; // y is now 10, but x is still valid (since it was Copy'd)
let z = x; // no use-after-move error, since the data is not moved!
Recommended Resources
Community Links
-
- Contains many links to other resources, guides, documentation, etc.
-
- Also check out the Community Discord
- Rust Forums
-
- Weekly updates on the Rust environment and community
- The
#rust-dev
channel in the Pitt CS Club Discord!
IDE Language Support
-
rust-analyzer is a must-have for beginners who want IDE support
- Supports many IDEs/text editors, such as VSCode/VSCodium, NeoVim, Emacs, Sublime Text 3, Eclipse, Kate Text Editor, and more, see the manual for all supported platforms.
- Much better than the "official" VSCode extension
- IntelliJ Rust - for IntelliJ-based IDEs
- Corrosion - for Eclipse-based IDEs
Beginner Guides
-
The Rust Programming Language by Steve Klabnik and Carol Nichols, with contributions from the Rust Community
-
Affectionately nicknamed “the book,” The Rust Programming Language will give you an overview of the language from first principles. You’ll build a few projects along the way, and by the end, you’ll have a solid grasp of the language.
-
-
-
Rust by Example (RBE) is a collection of runnable examples that illustrate various Rust concepts and standard libraries.
-
-
-
Small exercises to get you used to reading and writing Rust code!
-
- A Gentle Introduction To Rust by Steve Donovan
- A half-hour to learn Rust by @fasterthanlime
- Learning Rust by Dumindu Madunuwan
- Tour of Rust
Other Guides
- Rust for C++ Programmers by Nick Cameron
- Rust for Embedded C Programmers
-
Learn Rust With Entirely Too Many Linked Lists
- In Rust, writing your own linked list from scratch is (infamously) complex
- Rust Design Patterns
-
- Rust on microcontrollers
-
- Rust in the web browser (via WebAssembly)
References
- Rust Language Cheat Sheet
- The Rust Standard Library
-
The Rust Community’s Crate Registry
- "crates" are the standard for packaging Rust libraries
- A curated list of Rust code and resources
Opinions
Tobias Hildebrandt - 2021-10-09
Though I am by no means an expert in Rust, I really appreciate the features of the language. The compile-time checks can be frustrating -- I often call debugging "boxing with the compiler" -- but it is very satisfying to understand what things are determined to be safe and why. It has really helped me to organize my code better and think "is this really a safe thing to do?", even when programming in other languages.
Though I probably wouldn't recommend Rust to an absolute beginner, it is my language of choice in terms of general-purpose programming. Of course, some language are better suited for specific purposes, such as Python for rapid prototyping or C for low-level portability. The number of things you can build with Rust nowadays is massive, ranging from fullstack web dev with Seed and Rocket to building an operating system such as Redox to making Discord bots with Serenity to creating faster command-line utilities such as ripgrep. I truly think that Rust will continue to grow in the coming years and gain a stronger foothold in industry, competing for market share in areas that currently use C++.