Table Of ContentTTHHEE OOFFFFIICCIIAALL COVERS
GGUUIIDDEE TTOO RRUUSSTT RUST 2018 TT HH EE RR UU SS TT
PPRROOGGRRAAMMMMIINNGG EDITION RUST 2018
PP
PP RR OO GG RR AA MM MM II NN GG
RR
OO
The Rust Programming Language is the official book on • Using Cargo, Rust’s built-in package manager, to LL AA NN GG UU AA GG EE
GG
Rust: an open source systems programming language build, test, and document your code and manage
that helps you write faster, more reliable software. Rust dependencies RRTT
offers control over low-level details (such as memory
• How best to use Rust’s advanced compiler with
usage) in combination with high-level ergonomics, AA
compiler-led programming techniques HH
eliminating the hassle traditionally associated with
MM
low-level languages. You’ll find plenty of code examples throughout the S T E V E K L A B N I K and C A R O L N I C H O L S ,
book, as well as three chapters dedicated to building MMEE with C O N T R I B U T I O N S from T H E R U S T C O M M U N I T Y
The authors of The Rust Programming Language, two
complete projects to test your learning: a number
members of the Rust Core Team, share their knowledge
guessing game, a Rust implementation of a command II
and experience to show you how to take full advantage
line tool, and a multithreaded server. NNRR
of Rust’s features—from installation to creating robust
and scalable programs. You’ll begin with basics like NEW TO THIS EDITION: An extended section on GG
COVERS
creating functions, choosing data types, and binding Rust macros, an expanded chapter on modules, and UU
RUS T
variables and then move on to more advanced appendixes on Rust development tools and editions LL
concepts, such as:
ABOUT THE AUTHORS AASS 2018
• Ownership and borrowing, lifetimes, and traits
Steve Klabnik leads the Rust documentation team and NN
TT
• Using Rust’s memory safety guarantees to build fast, is one of Rust’s core developers. A frequent speaker
GG
safe programs and prolific open source contributor, he previously
• Testing, error handling, and effective refactoring worked on projects such as Ruby and Ruby on Rails. UU
Carol Nichols is a member of the Rust Core Team,
• Generics, smart pointers, multithreading, trait objects, co-founder of Integer 32, LLC, and organizer of the AA
and advanced pattern matching Rust Belt Rust Conference. GG
EE
N K
THE FINEST IN GEEK ENTERTAINMENT™ I L
CaA
www.nostarch.com $39.95 ($53.95 CDN) HnB
OdN
L I
LANGUAGES/RUSTSHELVE IN: PROGRAM S K
M
IN
G
THE RUST PROGRAMMING LANGUAGE
T H E R U S T
P R O G R A M M I N G
L A N G U A G E
by Steve Klabnik and Carol Nichols,
with contributions from
the Rust Community
San Francisco
THE RUST PROGRAMMING LANGUAGE. Copyright © 2019 by Mozilla Corporation and
the Rust Project Developers.
This edition has been updated to cover Rust 2018.
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-7185-0044-0
ISBN-13: 978-1-7185-0044-0
Publisher: William Pollock
Production Editor: Janelle Ludowise
Cover Illustration: Karen Rustad Tölva
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; info@nostarch.com
www.nostarch.com
The Library of Congress has catalogued the first edition as follows:
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 Rust’s core
developers. A frequent speaker and a prolific open source contributor, he
previously worked on projects such as Ruby and Ruby on Rails.
Carol Nichols is a member of the Rust Core Team and co-founder of
Integer 32, LLC, the world’s first Rust-focused software consultancy.
Nichols organizes the Rust Belt Rust Conference.
BRIEF CONTENTS
Foreword by Nicholas Matsakis and Aaron Turon . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxiii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
Chapter 1: Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Chapter 2: Programming a Guessing Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Chapter 3: Common Programming Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Chapter 4: Understanding Ownership . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Chapter 5: Using Structs to Structure Related Data . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Chapter 6: Enums and Pattern Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Chapter 7: Managing Growing Projects with Packages, Crates, and Modules . . . . . . . 111
Chapter 8: Common Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Chapter 9: Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Chapter 10: Generic Types, Traits, and Lifetimes . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Chapter 11: Writing Automated Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
Chapter 12: An I/O Project: Building a Command Line Program . . . . . . . . . . . . . . . . 233
Chapter 13: Functional Language Features: Iterators and Closures . . . . . . . . . . . . . . . 263
Chapter 14: More About Cargo and Crates .io . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Chapter 15: Smart Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
Chapter 16: Fearless Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Chapter 17: Object-Oriented Programming Features of Rust . . . . . . . . . . . . . . . . . . . 371
Chapter 18: Patterns and Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
Chapter 19: Advanced Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
Chapter 20: Final Project: Building a Multithreaded Web Server . . . . . . . . . . . . . . . . 457
Appendix A: Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
Appendix B: Operators and Symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
Appendix C: Derivable Traits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507
Appendix D: Useful Development Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511
Appendix E: Editions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517
viii Brief Contents