A guide to aid you in your journey of becoming a Rustacean (Rust developer). See the Contributing and Code of Conduct for more information about how to contribute to this repository.
Rust is a modern systems programming language with safety in mind as one of its core goals and strengths. Systems programming is programming within a resource contrained environment. However, as a lot of our services run now in paid-for-usage cloud environments, we can also consider them as resource constrained environments. This is why Rust is a great fit for more use cases then people might realize.
As it is a modern language and has taken the lessons from many other languages before it, it is also surprisingly pleasant to use once you get the hang of it. Its type system also allows expressive code that can help you exclude a great categories of bugs beyond the benefits that static typing can bring.
The goal of this guide is to introduce Rust to you as an individual or an entire organization. Should this not be sufficient you can also contact me for 1-on-1 coaching or workshops for your organization, by emailing me at glen@plabayo.tech.
Rust 101 Course
The origins of this guide can be found in the preparation of a semester long course I gave to a group of employees at OTA Insight Ltd.. The recordings of this course are available on YouTube at: https://www.youtube.com/playlist?list=PLQgXEsLXFxpVyLddG8FFXfNQEiodTzAjj.
This course is still actively being thought and the videos will be uploaded weekly when I find time to upload the recordings.
Learning Rust in Milestones
┌───────────────────────────┐
│ │
┌───────┴──────────┐ │
┌───────────┬─────────────►│2. Learn More Rust├─────┐ │
│ │ └────────┬─────────┘ │ │
│ ┌─────┴───────┐ │ ▲ │ │
│ │1. Learn Rust│ │ │ │ │
│ └──┬──┬───────┘ ▼ │ │ │
│ │ │ ┌────────────┴──────┐ │ │
│ │ └─────────────►│3. Learn Async Rust│ │ │
│ │ └─────────┬─────────┘ │ │
│ │ │ │ │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ ┌────────────────────────────────────────────────────┐ │
│ │4. Study Using the "Zero to Production in Rust" book│◄──┤
│ └──────────────────────────────┬─────────────────────┘ │
│ ▲ │ │
│ │ │ │
│ │ ▼ │
│ ┌─────────────────────────────┴─────────────────────────┐ │
│ │5. Contribute for the first time to an existing project│◄──┘
│ └──┬─────────────────────────────┬──────────────────────┘
│ │ │
│ │ ▼
│ │ ┌───────────────────────────────────────────────┐
│ │ │ 6. Study using the "Rust for Rustaceans" Book │
│ │ └────────────────────────┬──────────────────────┘
│ │ ▲ │
│ ▼ │ ▼
│ ┌──────────────────────────────┴────────────────────────────┐
└─┤7. Contribute an advanced feature or start your own project│
└──────────────────────────────────┬────────────────────────┘
│
▼
┌──────────────────────┐
│ 8. Continue to Learn │
└──────────────────────┘
Continiously Learn, Take Your Time
Enjoy the road :)
Sponsors
Support this project by becoming a sponsor.
Index
- About
- 1. Learn Rust
- 2. Learn More Rust
- 3. Learn Async Rust
- 4. Study using the “Zero to Production in Rust” book
- 5. Contribute for the first time to an existing project
- 6. Study using the “Rust for Rustaceans: Idiomatic Programming for Experienced Developers” book
- 7. Contribute an advanced feature to an existing project or start a project from scratch
- Appendix:
About
Glen De Cauwsemaecker created this Educational track to help you get started with Rust from an absolute beginner all the way until you’re an expert in it. He is open to mentor people, lead workshops and more. If you’re a team lead that wants to introduce Rust to their team, feel free to talk to him. Glen started with Rust around 2015, when Rust was still unstable and fast moving. Coming from a C++ background in a system programming job the benefits were immediately clear to him. In the meanwhile things have changed a lot and many companies have been starting to adopt Rust in their toolset:
- Amazon Web Services (AWS) has used Rust since 2017 for its serverless computing offerings, AWS Lambda and AWS Fargate. With that, Rust has gained further inroads. The company has written the Bottlerocket OS and the AWS Nitro System to deliver its Elastic Compute Cloud (EC2) service.
- Cloudflare develops many of its services, including its public DNS, serverless computing, and packet inspection offerings with Rust.
- Dropbox rebuilt its backend warehouse, which manages exabytes of storage, with Rust.
- Google develops parts of Android, such as its Bluetooth module, with Rust. Rust is also used for the crosvm component of Chrome OS and plays an important role in Google’s new operating system, Fuchsia.
- Facebook uses Rust to power Facebook’s web, mobile, and API services, as well as parts of HHVM, the HipHop virtual machine used by the Hack programming language.
- Microsoft writes components of its Azure platform including a security daemon for its Internet of Things (IoT) service in Rust.
- Mozilla uses Rust to enhance the Firefox web browser, which contains 15 million lines of code. Mozilla’s first two Rust-in-Firefox projects, its MP4 metadata parser and text encoder/decoder, led to overall performance and stability improvements.
- GitHub’s npm, Inc., uses Rust to deliver “upwards of 1.3 billion package downloads per day.”
- Oracle developed a container runtime with Rust to overcome problems with the Go reference implementation.
- Samsung, via its subsidiary SmartThings, uses Rust in its Hub, which is the firmware backend for its Internet of Things (IoT) service.
- The U.S. Department of Commerce’s National Institute of Standards and Technology (NIST) has added Rust to its list of “Safer Languages” as part of its Software Assurance Metrics and Tool Evaluation (SAMATE).
The language and its ecosystem have also very matured. Concepts like Async have also landed and Async runtimes such as Tokio have become stable and can be used without fear. While it was harder to convince companies to jump on the Rust wagon in the past, by now it should be a lot easier to sell. Is it one language to replace them all? Of course not, but neither should it be overlooked. Are you not sure how Rust might benefit your team for one thing or another? Contact Glen and figure it out together.
If you are a bit of a history nerd you might also enjoy:
- this article: How Rust went from a side project to the world’s most-loved programming language
- or this podcast: History of Rust with Ben Striegel — Rustacean Station
There’s a last section on the end of the Rust learning content.
This one contains useful references and sources that you can check as part of your
continuous journey as a capable Rust developer.
In case you are a Python or Javascript developer you might find the Appendix V. Python / Javascript developers section useful.
1. Learn Rust
In case you’re new to the language we suggest you to take a look at Learn Rust (free):
- The Rust Programming Language – The Rust Programming Language gives you a very nice overview;
- There are plenty of exercises in the book and at the end of your learning journey you’ll get to build your own multi-threaded web server!
- GitHub – rust-lang/rustlings: Small exercises to get you used to reading and writing Rust code! is an alternative more pragmatic approach on learning the language;
- Introduction – Rust By Example is there for people who like learning from real-world examples;
ⓘ Hardcore alternative
Programming Rust, 2nd Edition (O’Reilly Publishing) is a big book and will take you some time to get through. It is not for the faint of heart. However… If you do choose for this route as an alternative to (1) Learn Rust, you will absolutely not regret it. In fact, your entire rest of the Journey will be a breeze.
The “Programming Rust, 2nd Edition” book is a gem and in case you can handle big dry Technical books such as these it is one that will give you just as much love back as the energy that you put into it. Take your time, get through it, play with it, and enjoy. In fact, it can be easily paired with Rustlings by doing the exercises linked to the content you’re reading. Rust by Example can also be added on top of that to see some more code related to the stuff your learning.
Soak it in. It’s a hardcore alternative, but if you’re up for it, it’s there for you to grab.
If you’ve never done any Systems programming before, this will be an especially helpful book given it will explain a lot of the magic you’ve encountered in your very protected professional life so far. You’re welcome.
Questions you should be able to answer at the end of this step:
- Rust is a Memory-Safe language. How? Why? In what way is this different from other Memory Safe languages such as Go?
- Rust is a performant language. Why can it claim this. How?
- How do you structure data in Rust?
- Is data passed by Reference or value in Rust? And what does it mean?
- Is data mutable or immutable by default? How do we make it the other way?
- What are traits and how are these related to the data models that you structure in Rust?
- How can you extend the behaviour of external types?
- Can you extend behaviour of external types using external traits? Why or why not?
- What is a constant? Where can we define these?
- What is a static variable? How is it different from a constant? What’s special about mutable variables?
- How does Rust support Async Rust? How do you use it? What are Futures?
- How do you achieve parallel programming in Rust?
- What’s the difference between async programming and parallel programming? How are they related?
- What is the borrow checker? What is ownership? How are these concepts related?
- Data can be moved and it can also be dropped. What do we mean with this? How do we have control over this?
- How do we achieve shared mutable references to the same data? How can this be done safe?
- What are lifetimes? How do they work? Can you give some examples of where these are used?
- What are generics? Why do we use them?
- What are Rust Macros? When do we use these? What type of Macros are there?
- How do you build Rust projects?
- How do we use dependencies of other developers into our projects? And how do we call such dependencies?
- What are sum types? What are product types?
- What is an Enum in Rust and how can we use it? How can we define it?
- What is pattern matching in Rust? How do we do it? How is it more powerful than a switch?
- How do we write tests in Rust? What kind of tests are there?
- What are closures and how are they related to functions? What they have in common? How do they differ?
- What kind of smart pointers does the Rust std library provide you? What are there use cases, differences, and so on?
- What is unsafe Rust? How do we use it? When do we use it? What guarantees we still get, even when using unsafe Rust?
Alternative or Companion resources
Welcome to Comprehensive Rust
2. Learn more Rust
- Introduction – The Cargo Book: Cargo is the Rust package manager. Cargo downloads your Rust package’s dependencies, compiles your packages, makes distributable packages, and uploads them to crates.io: Rust Package Registry, the Rust community’s package registry;
- Getting started – Command Line Applications in Rust: Gives you another practical approach in your Rust Learning, by specifically learning how to write CLI applications in it;
Questions you should be able to answer at the end of this step:
- Make sure to be able to answer all questions of step (1).
- What is cargo and why does it exist?
- What kind of extensions exist for Cargo, how do we install them, how do we use them and what are some commonly used ones?
- What is the Cargo.toml file?
- What is the Cargo.lock file?
- What are build scripts? Why do we use these? How do we use these?
- How can we build code? How do we test code?
- How can we check code without building the final binaries?
- How do we make a simple CLI application? How do we pass arguments and flags? How do we handle signals?
- How can we structure our code and package(s)?
- What does public and private mean in the context of Rust? At what scope do these operate?
Learn More Rust: Extra
Once you went through the resources in the previous two chapters you might want to start working on a real project to start applying and solidifying your knowledge. A classic way to do this is by porting a small specialized codebase from one language to Rust, keeping in mind to not 1-to-1 map the lines of code but to instead focus on porting the logic with the “Rust way to do things” in mind.
Should you want some inspiration or a more guided approach, here are some resources that could help you out with this extra optional, but recommended step:
- Command-Line Rust: build a cli tool such as grep in rust, using test driven development (TDD), all to learn Rust
- Rust Application Books – The Little Book of Rust Books: lists a lot of different books, including ones that allow you to develop small projects using Rust, usually in a well guided fashion;
- Introduction – PNGme: An Intermediate Rust Project is an especially fun small project. It allows you to apply the knowledge that you learned above in a very narrow program, that is for once not network related. At the end you’ll have a cli tool that allows you to encode and decode “hidden” messages in a PNG image;
- Introduction – MacroKata: Macro’s are in general not very covered in the resources above, as it is not that common that you have to write your own macros. They can however be convenient in certain situations. This book can be seen as a bootstrap tutorial to get you started with one kind of Rust macros.
- If you are interested in (also) learning procedural macros you can perhaps consult the following resources:
- GitHub – dtolnay/proc-macro-workshop: Learn to write Rust procedural macros [Rust Latam conference, Montevideo Uruguay, March 2019]
- Procedural Macros in Rust (part 1)
- Check out “Diesel is a Safe, Extensible ORM and Query Builder for Rust” if you want to see a very cool example of macros (besides all the derives, formatting and printing you might already have been doing)
- json in serde_json – Rust is another neat example as it allows you to quickly define a Json value directly in Rust
- Taking a couple of hours is also a life well spent reading through this codebase as it will teach you a lot of nice things about Rust, especially given how simple and scoped the problem of json serialization is. It will really show you and teach you how serde works and why it works;
- Most web frameworks (if not all) in Rust also heavily make use of Macro’s to give you a very neat and clean API
- And of course do not forget to consult the “classic” macro Rust book: The Little Book of Rust Macros
- If you want to get a quick intro and overview of Macros this video can help a lot as well: Rust’s Witchcraft
- If you are interested in (also) learning procedural macros you can perhaps consult the following resources:
If you have a lot of free time at your hand and you want to build something really cool that is totally useless, here are some more ideas:
- Build your own Gameboy (classic) emulator: Introduction – DMG-01: How to Emulate a Game Boy
- Build your own CHIP-8 (8-bit computer) interpreter: Cowgod’s Chip-8 Technical Reference (no Rust guidance, directly from the CHIP-8 specification instead, you can do it, I believe in you)
- Build your own Ray Tracer (not a Rust tutorial, but can easily be implemented in Rust as well): Ray Tracing in One Weekend Series (fun if you always had the dream to start dabbling into the world of Graphics Programming)
Seriously though, do not consider the above recommended or mandatory in any way. If you however really like to develop stuff and you do like to do it extensively in your free time, then, and only then, I do believe that the above are a great way to really solidify your current Rust knowledge and give yourself a great (pragmatic) foundation.
Code like a pro in Rust
At this point of your Rust learning journey you’ve come a far way already, relatively speaking. Perhaps you want to shortcut, at least for now.
If so, the still to be published “Code like a pro in Rust” book might be enough for you to quickly get from beginner to advanced Rust programmer.
A Crust of Rust
Once you get at this point you are around the level of a beginner or intermediate Rust programmer. At this point you could already start to tackle stuff like “Rust for Rustaceans” of step (6). However it might be a bit much.
Therefore while you go through the next couple of sections it could be helpful to now and then (e.g. while washing dishes or instead of Netflix) an education Rust code video. In the appendix at the bottom of this page there a couple of such suggestions.
Particular to this context you might want to save the “Crust of Rust” playlist. Each video will go over a specific topic, e.g. Lifetimes, macros, Atomics, … No need to watch them one by one, feel free to jump to them in whatever orders and only those that interest you (which is the same tip that I can give for the “Rust for Rustaceans” book).
3. Learn Async Rust
There is no standard / official Asynchronous runtime. This in contrast to actual CPU threads which you can create from within the std library.
The most popular and recommended by many Async Runtime is the Tokio runtime. Which brings us to the next chapter:
- To start with your Async Learning Journey you might want to start with: Getting Started – Asynchronous Programming in Rust;
- Afterwards you can finally start going through the Tokio learning journey Tutorial | Tokio – An asynchronous Rust runtime
Please also join the Tokio Discord community as part of your async Journey, a place where you can ask any questions you want as well: https://discord.com/invite/tokio. It will be a place you want to be in once you start working for the Crawler team on RSProxy and its dependencies.
As an extra you may also want to read and develop alongside the following articles:
ⓘ Note: Tokio also provides support to the new Linux kernel io_uring concept, a new powerful way to allow async programming on Linux. Support for it can be found at: GitHub – tokio-rs/tokio-uring: An io_uring backed runtime for Rust
Synacktiv made an IO network scanner using io_uring as can be seen at GitHub – synacktiv/io_uring_scanner: io_uring based network scanner written in Rust, using tokio’s low level userspace bindings to io_uring.
Questions you should be able to answer at the end of this step:
- How does Async programming work in Rust, if you didn’t understand this already?
- What types of concurrent programming does Rust support? How many are there in general?
- What are Futures and how do they work? What function they have? How do they affect you?
- What is Send and Sync? What is it used for? How does it affect you?
- What are good use cases of Async code? What kind of tasks aren’t a good fit for this?
- What are Async Executors? What are Async Runtimes?
- What is Tokio? How do you use it? What does it give you?
- What are some other libraries in the Tokio ecosystem?
- What can you build with Tokio?
- Why would you use Tokio?
- How do you achieve Rust programming with only standard code (meaning code developed by the Rust core team)?
- Why does Rust not bundle an Async runtime?
- What is parallel programming? How do you do it in Rust? How does it relate to Async programming?
Tower
Once you step into the world of Async Rust, Tokio and Web Services you’ll sooner or later come across Tower, in a similar fashion as Serde blew your mind away for your serialization needs. Yes it did, you can admit it, this is a safe space.
Tower is a library of modular and reusable components for building robust networking clients and servers.
With Tower everything is a service. A service takes in a request and outputs either an error or a response.
What the actual types for these request-response pairs are, is up to you and depend mostly on the layer it is used.
In tower it is typical that a service wraps another service, called “layers”, as such all your services and middleware will stack on top of each other,
like a… tower.
- if you are new to Tower you can start learning how to use it by building your own tower middleware from scratch by following the guide at: https://github.com/tower-rs/tower/blob/master/guides/building-a-middleware-from-scratch.md;
- if you are not convinced on the usefulness of Tower you perhaps read to the “inventing your own Service trait” guide.
Rust Atomics and Locks
Understanding concurrency, the primitives used to do so safely and how it all works “under the hood” (e.g. the OS level) is not critical but it will help you big time the rest of your Async professional career.
The book “Rust Atomics and Locks” by O’Reilly is an amazingly good book by Mara Bos, an important Rust Contributor. Going through the book will take you some time despite its smaller size, but doing so is very rewarding. As with any technical book it is best to read this book as well as do its exercises and apply its knowledge using one program or another. Learn, Apply, Repeat. Play with it, Understand it.
This book is so good, that even Go, Python or JS developers that will never ever touch Rust, might want to learn Rust just to understand the knowledge that can be found in this Gem of a book. Read it. You won’t regret it.
As it is more advanced knowledge it can however be seen as “Extra”, so in case you are in a hurry to finish section (3) of the Rust track, feel free to skip it. But even if so, keep its existence in mind and come perhaps back to it when you start to get yourself into trouble.
Questions you should be able to answer at the end of this extra step:
- What primitives are there in general to achieve concurrency?
- What primitives does the OS give you?
- What is the difference between threads and async programming?
- Why is async programming faster?
- When would you use threads? When would you use async programming?
- What’s a Mutex? How could you implement one?
- What’s a Conditional Variable? How could you implement one?
- What’s a Signal? How could you implement one?
- What’s a Channel? How do you implement it? What types of channels are there?
- What is Reordering? How can we deal with this? How does it af