I love learning new programming languages.
I’ve been writing code for a long time, but I still find myself occasionally banging my head against a wall when learning new languages or tools. Rust is a unique language in many respects, and it introduces several concepts that some people may have never seen before, even if they have been programming for many years.
I have spent a great deal of time working with Rust both professionally and as a contributor to community projects, and I have written this book to share what I learned along the way. By taking the time to read this book, you will save yourself a lot of time by avoiding the common pitfalls and problems people encounter when they’re new to Rust.
I’d like to thank my friends Javeed Shaikh and Ben Lin for providing feedback on early drafts of the manuscript as well as Manning Publications for working patiently with me in completing this book.
Specifically, I would like to thank the development editor Karen Miller, the review editor Aleksandar Dragosavljević, the production editor Deirdre Hiam, the copyeditor Christian Berk, and the proofreader Katie Tennant.
I thank all the reviewers—Adam Wendell, Alessandro Campeis, Arun Bhagvan Kommadi, Christian Witts, Clifford Thurber, David Moshal, David Paccoud, Gianluigi Spagnuolo, Horaci Macias, Jaume Lopez, Jean-Paul Malherbe, João Pedro de Lacerda, Jon Riddle, Joseph Pachod, Julien Castelain, Kevin Orr, Madhav Ayyagari, Martin Nowack, Matt Sarmiento, Matthew Winter, Matthias Busch, PK Chetty, Rohit Goswami, Satadru Roy, Satej Kumar Sahu, Sebastian Palma, Seth MacPherson, Simon Tschöke, Sri Kadimisetty, Tim van Deurzen, William Wheeler, and Zach Peters—your suggestions help to make this a better book.
While writing this book, I’ve paid special attention to noting when features or interfaces are subject to change. While core language features may not change substantially, practical use of Rust may involve hundreds of separate libraries and projects. By reading this book, you’ll be introduced to strategies and techniques that help you navigate this evolving ecosystem.
As you continue to read, I would like to mention that the Rust ecosystem is rapidly evolving. I have written this book with the future of Rust in mind, but I cannot guarantee that the language and libraries won’t change substantially after the book has been published.
This book focuses on the practical usage of Rust, while considering the big-picture themes, limitations of Rust and its tooling, and how developers can become productive with Rust quickly. The text is not an introduction to the Rust language, nor is it a replacement for the official Rust documentation. This book supplements the existing documentation and resources available for Rust and provides the most important lessons you won’t find in Rust documentation in one place. While Code Like a Pro in Rust does not provide an exhaustive reference for the Rust language, I do indicate where to go for additional information when appropriate.
Readers of this book should be familiar with Rust and consider themselves beginner to intermediate Rust programmers. If you have never used Rust, you may find this book difficult to read, as it contains many references to Rust-specific features, for which I will not spend much time discussing the background. If you find yourself confused about Rust syntax or technical details, I recommended starting with Rust in Action by Tim McNamara (Manning, 2021) or the official Rust book at https://doc.rust-lang.org/book/.
For those who are intermediate to advanced Rust programmers, some content in this book might be familiar. In your case, I recommend you skip ahead to the chapters most interesting to you.
The chapters of this book can be read in any order, according to what interests you most. While I would like for every reader to read every chapter from start to finish, I also understand that readers have varied goals and experience levels. Most of the later chapters build on top of content from earlier chapters, so while it isn’t a requirement, you’ll get the most benefit from reading the chapters in order. Throughout the book, I make references to other chapters or topics as needed, and you may want to make notes to go back and read other sections accordingly.
If you are relatively new to Rust or programming in general, then I recommend reading the whole book from cover to cover, to get the most out of it. For an excellent introduction to the Rust programming language, I recommend you first read the official Rust book (https://doc.rust-lang.org/book/).
Part 1 covers an introduction to Rust and its tooling:
Part 2 covers Rust’s data structures and memory management:
Part 3 discusses testing methods for correctness:
Part 4 introduces asynchronous Rust programming:
Part 5 discusses optimizations:
This book contains many examples of source code both in numbered listings and in line with normal text. In both cases, source code is formatted in a fixed-width font like this
to separate it from ordinary text. Sometimes code is also in bold
to highlight code that has changed from previous steps in the chapter, such as when a new feature adds to an existing line of code.
In many cases, the original source code has been reformatted; we’ve added line breaks and reworked indentation to accommodate the available page space in the book. In some cases, even this was not enough, and listings include line-continuation markers (➥). Additionally, comments in the source code have often been removed from the listings when the code is described in the text. Code annotations accompany many of the listings, highlighting important concepts.
You can get executable snippets of code from the liveBook (online) version of this book at https://livebook.manning.com/book/code-like-a-pro-in-rust. The complete code for the examples in the book is available for download from the Manning website at https://www.manning.com/books/code-like-a-pro-in-rust, and from GitHub at https://github.com/brndnmtthws/code-like-a-pro-in-rust-book.
You can clone a copy of the book’s code locally on your computer by running the following command with Git:
$ git clone https://github.com/brndnmtthws/code-like-a-pro-in-rust-book
The book’s code is organized into directories by chapter and section within the repository, organized within each section by topic. The code is licensed under the MIT license, which is a permissive license that allows you to copy the code samples and use them as you see fit, even as the basis for your own work.
Throughout this book, there are many references to open source projects which are used as teaching aids. The source code for most of these projects (or crates) can be obtained from their respective project repositories—for reference, see the following table.
List of projects referenced in this book
Purchase of Code Like a Pro in Rust includes free access to liveBook, Manning’s online reading platform. Using liveBook’s exclusive discussion features, you can attach comments to the book globally or to specific sections or paragraphs. It’s a snap to make notes for yourself, ask and answer technical questions, and receive help from the author and other users. To access the forum, go to https://livebook.manning.com/book/code-like-a-pro-in-rust. You can also learn more about Manning’s forums and the rules of conduct at https://livebook.manning.com/discussion.
Manning’s commitment to our readers is to provide a venue where a meaningful dialogue between individual readers and between readers and the author can take place. It is not a commitment to any specific amount of participation on the part of the author, whose contribution to the forum remains voluntary (and unpaid). We suggest you try asking him some challenging questions lest his interest stray! The forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print.
Brenden Matthews is a software engineer, entrepreneur, and prolific open source contributor. He has been using Rust since the early days of the language and has contributed to several Rust tools and open source projects, in addition to using Rust professionally. He’s the author of Conky, a popular system monitor, and a member of the Apache Software Foundation, with over 25 years of industry experience. Brenden is also a YouTube contributor and instructor as well as an author of many articles on Rust and other programming languages. He has given talks at several technology conferences, including QCon, LinuxCon, ContainerCon, MesosCon, All Things Open, and Rust meetups. He has been a GitHub contributor for over 13 years, has multiple published Rust crates, has contributed to several open source Rust projects, and has built production-grade Rust applications professionally. Brenden is also the author of Rust Design Patterns, a follow-up to Code Like a Pro in Rust.
The figure on the cover of Code Like a Pro in Rust is “Femme de l’Argou,” or “Woman of Aargau, Switzerland,” taken from a collection by Jacques Grasset de Saint-Sauveur, published in 1797. Each illustration is finely drawn and colored by hand.
In those days, it was easy to identify where people lived and what their trade or station in life was just by their dress. Manning celebrates the inventiveness and initiative of the computer business with book covers based on the rich diversity of regional culture centuries ago, brought back to life by pictures from collections such as this one.