Programming Rust
by Jim Blandy , Jason Orendorff , and Leonora F.S. Tindall
Copyright 2021 Jim Blandy, Leonora F.S. Tindall, Jason Orendorff. All rights reserved.
Printed in the United States of America.
Published by OReilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
OReilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com .
- Acquisitions Editor: Suzanne McQuade
- Developmental Editor: Jeff Bleiel
- Production Editor: Beth Kelly
- Copyeditor: Charles Roumeliotis
- Proofreader: Kim Wimpsett
- Indexer: Potomac Indexing, LLC
- Interior Designer: David Futato
- Cover Designer: Karen Montgomery
- Illustrator: Kate Dullea
- June 2021: Second Edition
Revision History for the Second Edition
- 2021-06-11: First Release
- 2021-11-05: Second Release
See http://oreilly.com/catalog/errata.csp?isbn=9781492052593 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. Programming Rust, the cover image, and related trade dress are trademarks of OReilly Media, Inc.
The views expressed in this work are those of the authors, and do not represent the publishers views. While the publisher and the authors have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the authors disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
978-1-492-05259-3
[LSI]
Preface
Rust is a language for systems programming.
This bears some explanation these days, as systems programming is unfamiliar to most working programmers. Yet it underlies everything we do.
You close your laptop. The operating system detects this, suspends all the running programs, turns off the screen, and puts the computer to sleep. Later, you open the laptop: the screen and other components are powered up again, and each program is able to pick up where it left off. We take this for granted. But systems programmers wrote a lot of code to make that happen.
Systems programming is for:
Operating systems
Device drivers of all kinds
Filesystems
Databases
Code that runs in very cheap devices, or devices that must be extremely reliable
Cryptography
Media codecs (software for reading and writing audio, video, and image files)
Media processing (for example, speech recognition or photo editing software)
Memory management (for example, implementing a garbage collector)
Text rendering (the conversion of text and fonts into pixels)
Implementing higher-level programming languages (like JavaScript and Python)
Networking
Virtualization and software containers
Scientific simulations
Games
In short, systems programming is resource-constrained programming. It is programming when every byte and every CPU cycle counts.
The amount of systems code involved in supporting a basic app is staggering.
This book will not teach you systems programming. In fact, this book covers many details of memory management that might seem unnecessarily abstruse at first, if you havent already done some systems programming on your own. But if you are a seasoned systems programmer, youll find that Rust is something exceptional: a new tool that eliminates major, well-understood problems that have plagued a whole industry for decades.
Who Should Read This Book
If youre already a systems programmer and youre ready for an alternative to C++, this book is for you. If youre an experienced developer in any programming language, whether thats C#, Java, Python, JavaScript, or something else, this book is for you too.
However, you dont just need to learn Rust. To get the most out of the language, you also need to gain some experience with systems programming. We recommend reading this book while also implementing some systems programming side projects in Rust. Build something youve never built before, something that takes advantage of Rusts speed, concurrency, and safety. The list of topics at the beginning of this preface should give you some ideas.
Why We Wrote This Book
We set out to write the book we wished we had when we started learning Rust. Our goal was to tackle the big, new concepts in Rust up front and head-on, presenting them clearly and in depth so as to minimize learning by trial and error.
Navigating This Book
The first two chapters of this book introduce Rust and provide a brief tour before we move on to the fundamental data types in address the core concepts of ownership and references. We recommend reading these first five chapters through in order.
Chapters ). Its all right to skim a little here, but dont skip the chapter on error handling. Trust us.
covers many more utility traits.
Understanding traits and generics unlocks the rest of the book. Closures and iterators, two key power tools that you wont want to miss, are covered in Chapters ).
Conventions Used in This Book
The following typographical conventions are used in this book:
ItalicIndicates new terms, URLs, email addresses, filenames, and file extensions.
Constant width
Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.
Constant width bold
Shows commands or other text that should be typed literally by the user.
Constant width italic
Shows text that should be replaced with user-supplied values or by values determined by context.
Note
This icon signifies a general note.
Using Code Examples
Supplemental material (code examples, exercises, etc.) is available for download at https://github.com/ProgrammingRust.
This book is here to help you get your job done. In general, if example code is offered with this book, you may use it in your programs and documentation. You do not need to contact us for permission unless youre reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing examples from OReilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your products documentation does require permission.