THE RUST PROGRAMMING LANGUAGE
by Steve Klabnik and Carol Nichols, with contributions from the Rust Community
San Francisco
THE RUST PROGRAMMING LANGUAGE. Copyright 2018 by Mozilla Corporation and the Rust Project Developers.
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher.
ISBN-10: 1-59327-828-4
ISBN-13: 978-1-59327-828-1
Publisher: William Pollock
Production Editor: Janelle Ludowise
Cover Illustration: Karen Rustad Tlva
Interior Design: Octopod Studios
Developmental Editor: Liz Chadwick
Technical Reviewers: Eduard-Mihai eddyb Burtescu and Alex Crichton
Copyeditor: Anne Marie Walker
Compositors: Meg Sneeringer and Janelle Ludowise
Proofreader: Paula L. Fleming
For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly:
No Starch Press, Inc.
245 8th Street, San Francisco, CA 94103
phone: 1.415.863.9900;
www.nostarch.com
Library of Congress Cataloging-in-Publication Data
Names: Klabnik, Steve, author. | Nichols, Carol, 1983- eauthor.
Title: The Rust programming language / by Steve Klabnik and Carol Nichols ;
with contributions from the Rust Community.
Description: San Francisco : No Starch Press, Inc., 2018. | Includes index.
Identifiers: LCCN 2018014097 (print) | LCCN 2018019844 (ebook) | ISBN
9781593278519 (epub) | ISBN 1593278519 (epub) | ISBN 9781593278281
(paperback) | ISBN 1593278284 (paperback)
Subjects: LCSH: Rust (Computer programming language) | BISAC: COMPUTERS /
Programming / Open Source. | COMPUTERS / Programming Languages / General.
| COMPUTERS / Programming / General.
Classification: LCC QA76.73.R87 (ebook) | LCC QA76.73.R87 K53 2018 (print) |
DDC 005.13/3--dc23
LC record available at https://lccn.loc.gov/2018014097
No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The information in this book is distributed on an As Is basis, without warranty. While every precaution has been taken in the preparation of this work, neither the authors nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.
About the Authors
Steve Klabnik leads the Rust documentation team and is one of Rusts core developers. A frequent speaker and a prolific open source contributor, he previously worked on projects such as Ruby and Ruby on Rails. Klabnik works at Mozilla.
Carol Nichols is a member of the Rust Core Team and co-founder of Integer 32, LLC, the worlds first Rust-focused software consultancy. Nichols organizes the Rust Belt Rust Conference.
BRIEF CONTENTS
CONTENTS IN DETAIL
1
GETTING STARTED
2
PROGRAMMING A GUESSING GAME
3
COMMON PROGRAMMING CONCEPTS
4
UNDERSTANDING OWNERSHIP
5
USING STRUCTS TO STRUCTURE RELATED DATA
6
ENUMS AND PATTERN MATCHING
7
USING MODULES TO REUSE AND ORGANIZE CODE
8
COMMON COLLECTIONS
9
ERROR HANDLING
10
GENERIC TYPES, TRAITS, AND LIFETIMES
11
WRITING AUTOMATED TESTS
12
AN I/O PROJECT: BUILDING A COMMAND LINE PROGRAM
13
FUNCTIONAL LANGUAGE FEATURES: ITERATORS AND CLOSURES
14
MORE ABOUT CARGO AND CRATES.IO
15
SMART POINTERS
16
FEARLESS CONCURRENCY
17
OBJECT-ORIENTED PROGRAMMING FEATURES OF RUST
18
PATTERNS AND MATCHING
19
ADVANCED FEATURES
20
FINAL PROJECT: BUILDING A MULTITHREADED WEB SERVER
A
KEYWORDS
B
OPERATORS AND SYMBOLS
C
DERIVABLE TRAITS
D
MACROS
FOREWORD
It wasnt always so clear, but the Rust programming language is fundamentally about empowerment: no matter what kind of code you are writing now, Rust empowers you to reach further, to program with confidence in a wider variety of domains than you did before.
Take, for example, systems-level work that deals with low-level details of memory management, data representation, and concurrency. Traditionally, this realm of programming is seen as arcane, accessible only to a select few who have devoted the necessary years learning to avoid its infamous pitfalls. And even those who practice it do so with caution, lest their code be open to exploits, crashes, or corruption.
Rust breaks down these barriers by eliminating the old pitfalls and providing a friendly, polished set of tools to help you along the way. Programmers who need to dip down into lower-level control can do so with Rust, without taking on the customary risk of crashes or security holes, and without having to learn the fine points of a fickle toolchain. Better yet, the language is designed to guide you naturally towards reliable code that is efficient in terms of speed and memory usage.
Programmers who are already working with low-level code can use Rust to raise their ambitions. For example, introducing parallelism in Rust is a relatively low-risk operation: the compiler will catch the classical mistakes for you. And you can tackle more aggressive optimizations in your code with the confidence that you wont accidentally introduce crashes or exploits.
But Rust isnt limited to low-level systems programming. Its expressive and ergonomic enough to make CLI apps, web servers, and many other kinds of code quite pleasant to writeyoull find simple examples of both later in the book. Working with Rust allows you to build skills that transfer from one domain to another; you can learn Rust by writing a web app and then apply those same skills to target your Raspberry Pi.
This book fully embraces the potential of Rust to empower its users. Its a friendly and approachable text intended to help you level up not just your knowledge of Rust but also your reach and confidence as a programmer in general. So dive in, get ready to learnand welcome to the Rust community!
Nicholas Matsakis and Aaron Turon
ACKNOWLEDGMENTS
We would like to thank everyone who has worked on the Rust language for creating an amazing language worth writing a book about. Were grateful to everyone in the Rust community for being welcoming and creating an environment worth welcoming more folks into.
Were especially thankful for everyone who read early versions of this book online and provided feedback, bug reports, and pull requests. Special thanks to Eduard-Mihai Burtescu and Alex Crichton for providing technical review and Karen Rustad Tlva for the cover art. Thank you to our team at No Starch, including Bill Pollock, Liz Chadwick, and Janelle Ludowise, for improving this book and bringing it to print.