Share E-Book

The Rust Programming Language (Covers Rust 2018) (Steve Klabnik, Carol Nichols)(Z-Library)

Author

,

Rating No ratings yet

Log in to rate

Programming Language
Language English

The official book on the Rust programming language, written by the Rust development team at the Mozilla Foundation, fully updated for Rust 2018. The Rust Programming Language is the official book on Rust: an open source systems programming language that helps you write faster, more reliable software. Rust offers control over low-level details (such as memory usage) in combination with high-level ergonomics, eliminating the hassle traditionally associated with low-level languages. The authors of The Rust Programming Language, members of the Rust Core Team, share their knowledge and experience to show you how to take full advantage of Rust's features--from installation to creating robust and scalable programs. You'll begin with basics like creating functions, choosing data types, and binding variables and then move on to more advanced concepts, such as: • Ownership and borrowing, lifetimes, and traits • Using Rust's memory safety guarantees to build fast, safe programs • Testing, error handling, and effective refactoring • Generics, smart pointers, multithreading, trait objects, and advanced pattern matching • Using Cargo, Rust's built-in package manager, to build, test, and document your code and manage dependencies • How best to use Rust's advanced compiler with compiler-led programming techniques You'll find plenty of code examples throughout the book, as well as three chapters dedicated to building complete projects to test your learning: a number guessing game, a Rust implementation of a command line tool, and a multithreaded server. New to this edition: An extended section on Rust macros, an expanded chapter on modules, and appendixes on Rust development tools and editions.

Format PDF
Size 8.7 MB
8
Views
(First 20 pages)

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Page 1
COVERS RUST 2018 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 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 , 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 COVERS RUST 2018 EDITION w w w.nostarch.com TH E F I N EST I N G E E K E NTE RTA I N M E NT™ The Rust Programming Language is the official book on Rust: an open source systems programming language that helps you write faster, more reliable software. Rust offers control over low-level details (such as memory usage) in combination with high-level ergonomics, eliminating the hassle traditionally associated with low-level languages. The authors of The Rust Programming Language, two members of the Rust Core Team, share their knowledge and experience to show you how to take full advantage of Rust’s features—from installation to creating robust and scalable programs. You’ll begin with basics like creating functions, choosing data types, and binding variables and then move on to more advanced concepts, such as: • Ownership and borrowing, lifetimes, and traits • Using Rust’s memory safety guarantees to build fast, safe programs • Testing, error handling, and effective refactoring • Generics, smart pointers, multithreading, trait objects, and advanced pattern matching SHELVE IN: PROGRAM M ING LANGUAGES/RUST • Using Cargo, Rust’s built-in package manager, to build, test, and document your code and manage dependencies • How best to use Rust’s advanced compiler with compiler-led programming techniques You’ll find plenty of code examples throughout the book, as well as three chapters dedicated to building complete projects to test your learning: a number guessing game, a Rust implementation of a command line tool, and a multithreaded server. N E W T O T H I S E D I T I O N : An extended section on Rust macros, an expanded chapter on modules, and appendixes on Rust development tools and editions A B O U T T H E A U T H O R S Steve Klabnik leads the Rust documentation team and is one of Rust’s core developers. A frequent speaker and 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, co-founder of Integer 32, LLC, and organizer of the Rust Belt Rust Conference. $39 .9 5 ($53.95 CDN) P R O G R A M M I N G G U I D E T O R U S T T H E O F F I C I A L K L A B N IK a n d N IC H O L S R U S T 2 0 1 8 TH E R U S T P R O G R A M M IN G L A N G U A G E TH E R U S T P R O G R A M M IN G L A N G U A G E
Page 2
(This page has no text content)
Page 3
THE RUST PROGRAMMING LANGUAGE
Page 4
(This page has no text content)
Page 5
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 contr ibut ions from the Rust Community San Francisco
Page 6
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.
Page 7
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.
Page 8
(This page has no text content)
Page 9
B R I E F C O N T E N T S 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
Page 10
viii Brief Contents 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
Page 11
C O N T E N T S I N D E T A I L FOREWORD by Nicholas Matsakis and Aaron Turon xix PREFACE xxi ACKNOWLEDGMENTS xxiii INTRODUCTION xxv Who Rust Is For . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxvi Teams of Developers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxvi Students . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxvi Companies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxvi Open Source Developers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxvi People Who Value Speed and Stability . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii Who This Book Is For . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii How to Use This Book. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii Resources and How to Contribute to This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxix 1 GETTING STARTED 1 Installation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Command Line Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Installing rustup on Linux or macOS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Installing rustup on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Updating and Uninstalling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Local Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Hello, World! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Creating a Project Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Writing and Running a Rust Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Anatomy of a Rust Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Compiling and Running Are Separate Steps . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Hello, Cargo! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Creating a Project with Cargo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Building and Running a Cargo Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Building for Release. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Cargo as Convention . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2 PROGRAMMING A GUESSING GAME 13 Setting Up a New Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Processing a Guess . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Storing Values with Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Handling Potential Failure with the Result Type . . . . . . . . . . . . . . . . . . . . . . . 17
Page 12
x Contents in Detail Printing Values with println! Placeholders . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Testing the First Part. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Generating a Secret Number. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Using a Crate to Get More Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Generating a Random Number. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Comparing the Guess to the Secret Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Allowing Multiple Guesses with Looping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Quitting After a Correct Guess . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Handling Invalid Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 3 COMMON PROGRAMMING CONCEPTS 31 Variables and Mutability. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Differences Between Variables and Constants. . . . . . . . . . . . . . . . . . . . . . . . 34 Shadowing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Scalar Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Compound Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Function Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Statements and Expressions in Function Bodies . . . . . . . . . . . . . . . . . . . . . . . 45 Functions with Return Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Control Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 if Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Repetition with Loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 4 UNDERSTANDING OWNERSHIP 59 What Is Ownership? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Ownership Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Variable Scope. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 The String Type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Memory and Allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Ownership and Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Return Values and Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 References and Borrowing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Mutable References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Dangling References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 The Rules of References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 The Slice Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 String Slices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Other Slices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Page 13
Contents in Detail xi 5 USING STRUCTS TO STRUCTURE RELATED DATA 83 Defining and Instantiating Structs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Using the Field Init Shorthand When Variables and Fields Have the Same Name. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Creating Instances from Other Instances with Struct Update Syntax . . . . . . . . . 86 Using Tuple Structs Without Named Fields to Create Different Types . . . . . . . . 86 Unit-Like Structs Without Any Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 An Example Program Using Structs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Refactoring with Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Refactoring with Structs: Adding More Meaning . . . . . . . . . . . . . . . . . . . . . . 89 Adding Useful Functionality with Derived Traits . . . . . . . . . . . . . . . . . . . . . . . 90 Method Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Defining Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Methods with More Parameters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Associated Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Multiple impl Blocks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 6 ENUMS AND PATTERN MATCHING 97 Defining an Enum. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Enum Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 The Option Enum and Its Advantages over Null Values . . . . . . . . . . . . . . . . 101 The match Control Flow Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 Patterns That Bind to Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Matching with Option<T> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Matches Are Exhaustive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 The _ Placeholder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Concise Control Flow with if let . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 7 MANAGING GROWING PROJECTS WITH PACKAGES, CRATES, AND MODULES 111 Packages and Crates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Defining Modules to Control Scope and Privacy. . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Paths for Referring to an Item in the Module Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Exposing Paths with the pub Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Starting Relative Paths with super . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Making Structs and Enums Public . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 Bringing Paths into Scope with the use Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Creating Idiomatic use Paths. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Providing New Names with the as Keyword. . . . . . . . . . . . . . . . . . . . . . . . 124 Re-exporting Names with pub use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Using External Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Using Nested Paths to Clean Up Large use Lists . . . . . . . . . . . . . . . . . . . . . 126 The Glob Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Separating Modules into Different Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Page 14
xii Contents in Detail 8 COMMON COLLECTIONS 131 Storing Lists of Values with Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 Creating a New Vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 Updating a Vector. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 Dropping a Vector Drops Its Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Reading Elements of Vectors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Iterating over the Values in a Vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Using an Enum to Store Multiple Types . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Storing UTF-8 Encoded Text with Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 What Is a String? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Creating a New String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Updating a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 Indexing into Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Slicing Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 Methods for Iterating over Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 Strings Are Not So Simple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Storing Keys with Associated Values in Hash Maps . . . . . . . . . . . . . . . . . . . . . . . . . 144 Creating a New Hash Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Hash Maps and Ownership . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Accessing Values in a Hash Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Updating a Hash Map. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Hashing Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 9 ERROR HANDLING 151 Unrecoverable Errors with panic! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 Using a panic! Backtrace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Recoverable Errors with Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Matching on Different Errors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 Shortcuts for Panic on Error: unwrap and expect . . . . . . . . . . . . . . . . . . . . . 159 Propagating Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 To panic! or Not to panic! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 Examples, Prototype Code, and Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 Cases in Which You Have More Information Than the Compiler . . . . . . . . . . 165 Guidelines for Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 Creating Custom Types for Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 10 GENERIC TYPES, TRAITS, AND LIFETIMES 171 Removing Duplication by Extracting a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 Generic Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 In Function Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 In Struct Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 In Enum Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 In Method Definitions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Performance of Code Using Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Page 15
Contents in Detail xiii Traits: Defining Shared Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 Defining a Trait. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 Implementing a Trait on a Type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 Default Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Traits as Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Returning Types that Implement Traits. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 Fixing the largest Function with Trait Bounds. . . . . . . . . . . . . . . . . . . . . . . . 189 Using Trait Bounds to Conditionally Implement Methods. . . . . . . . . . . . . . . . 191 Validating References with Lifetimes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 Preventing Dangling References with Lifetimes. . . . . . . . . . . . . . . . . . . . . . . 193 The Borrow Checker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 Generic Lifetimes in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 Lifetime Annotation Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 Lifetime Annotations in Function Signatures. . . . . . . . . . . . . . . . . . . . . . . . . 197 Thinking in Terms of Lifetimes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 Lifetime Annotations in Struct Definitions. . . . . . . . . . . . . . . . . . . . . . . . . . . 200 Lifetime Elision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 Lifetime Annotations in Method Definitions . . . . . . . . . . . . . . . . . . . . . . . . . 203 The Static Lifetime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Generic Type Parameters, Trait Bounds, and Lifetimes Together . . . . . . . . . . . . . . . . . 205 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 11 WRITING AUTOMATED TESTS 207 How to Write Tests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 The Anatomy of a Test Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 Checking Results with the assert! Macro. . . . . . . . . . . . . . . . . . . . . . . . . . . 211 Testing Equality with the assert_eq! and assert_ne! Macros . . . . . . . . . . . . . 214 Adding Custom Failure Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 Checking for Panics with should_panic . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 Using Result<T, E> in Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 Controlling How Tests Are Run. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 Running Tests in Parallel or Consecutively. . . . . . . . . . . . . . . . . . . . . . . . . . 222 Showing Function Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 Running a Subset of Tests by Name. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 Ignoring Some Tests Unless Specifically Requested . . . . . . . . . . . . . . . . . . . 226 Test Organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 Integration Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 12 AN I/O PROJECT: BUILDING A COMMAND LINE PROGRAM 233 Accepting Command Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 Reading the Argument Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 Saving the Argument Values in Variables . . . . . . . . . . . . . . . . . . . . . . . . . . 236 Reading a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 Refactoring to Improve Modularity and Error Handling . . . . . . . . . . . . . . . . . . . . . . . 238 Separation of Concerns for Binary Projects. . . . . . . . . . . . . . . . . . . . . . . . . 239 Fixing the Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Page 16
xiv Contents in Detail Extracting Logic from main . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 Splitting Code into a Library Crate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 Developing the Library’s Functionality with Test-Driven Development . . . . . . . . . . . . . . 250 Writing a Failing Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 Writing Code to Pass the Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 Working with Environment Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 Writing a Failing Test for the Case-Insensitive search Function . . . . . . . . . . . 255 Implementing the search_case_insensitive Function . . . . . . . . . . . . . . . . . . . 257 Writing Error Messages to Standard Error Instead of Standard Output . . . . . . . . . . . . 260 Checking Where Errors Are Written . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260 Printing Errors to Standard Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 13 FUNCTIONAL LANGUAGE FEATURES: ITERATORS AND CLOSURES 263 Closures: Anonymous Functions That Can Capture Their Environment . . . . . . . . . . . . . 264 Creating an Abstraction of Behavior with Closures . . . . . . . . . . . . . . . . . . . 264 Closure Type Inference and Annotation . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 Storing Closures Using Generic Parameters and the Fn Traits . . . . . . . . . . . . 270 Limitations of the Cacher Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . 273 Capturing the Environment with Closures . . . . . . . . . . . . . . . . . . . . . . . . . . 274 Processing a Series of Items with Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 The Iterator Trait and the next Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 Methods That Consume the Iterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278 Methods That Produce Other Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 Using Closures That Capture Their Environment. . . . . . . . . . . . . . . . . . . . . . 280 Creating Our Own Iterators with the Iterator Trait . . . . . . . . . . . . . . . . . . . . 281 Improving Our I/O Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 Removing a clone Using an Iterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 Making Code Clearer with Iterator Adaptors . . . . . . . . . . . . . . . . . . . . . . . 286 Comparing Performance: Loops vs. Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 14 MORE ABOUT CARGO AND CRATES.IO 291 Customizing Builds with Release Profiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292 Publishing a Crate to Crates.io . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 Making Useful Documentation Comments. . . . . . . . . . . . . . . . . . . . . . . . . . 293 Exporting a Convenient Public API with pub use . . . . . . . . . . . . . . . . . . . . . 296 Setting Up a Crates.io Account . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300 Adding Metadata to a New Crate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300 Publishing to Crates.io . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 Publishing a New Version of an Existing Crate . . . . . . . . . . . . . . . . . . . . . . 302 Removing Versions from Crates.io with cargo yank . . . . . . . . . . . . . . . . . . . 302 Cargo Workspaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 Creating a Workspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 Creating the Second Crate in the Workspace . . . . . . . . . . . . . . . . . . . . . . . 304
Page 17
Contents in Detail xv Installing Binaries from Crates.io with cargo install . . . . . . . . . . . . . . . . . . . . . . . . . . 308 Extending Cargo with Custom Commands. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309 15 SMART POINTERS 311 Using Box<T> to Point to Data on the Heap. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312 Using a Box<T> to Store Data on the Heap . . . . . . . . . . . . . . . . . . . . . . . . 313 Enabling Recursive Types with Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314 Treating Smart Pointers Like Regular References with the Deref Trait . . . . . . . . . . . . . . 317 Following the Pointer to the Value with the Dereference Operator . . . . . . . . . 318 Using Box<T> Like a Reference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318 Defining Our Own Smart Pointer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 Treating a Type Like a Reference by Implementing the Deref Trait . . . . . . . . . 320 Implicit Deref Coercions with Functions and Methods. . . . . . . . . . . . . . . . . . 321 How Deref Coercion Interacts with Mutability . . . . . . . . . . . . . . . . . . . . . . . 322 Running Code on Cleanup with the Drop Trait . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323 Dropping a Value Early with std::mem::drop . . . . . . . . . . . . . . . . . . . . . . . 325 Rc<T>, the Reference Counted Smart Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326 Using Rc<T> to Share Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327 Cloning an Rc<T> Increases the Reference Count . . . . . . . . . . . . . . . . . . . . 329 RefCell<T> and the Interior Mutability Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330 Enforcing Borrowing Rules at Runtime with RefCell<T>. . . . . . . . . . . . . . . . . 330 Interior Mutability: A Mutable Borrow to an Immutable Value . . . . . . . . . . . . 331 Having Multiple Owners of Mutable Data by Combining Rc<T> and RefCell<T> . . . . . . . . . . . . . . . . . . . . . . . . . . . 337 Reference Cycles Can Leak Memory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 Creating a Reference Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 Preventing Reference Cycles: Turning an Rc<T> into a Weak<T> . . . . . . . . . 341 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346 16 FEARLESS CONCURRENCY 347 Using Threads to Run Code Simultaneously . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348 Creating a New Thread with spawn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350 Waiting for All Threads to Finish Using join Handles . . . . . . . . . . . . . . . . . . 351 Using move Closures with Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353 Using Message Passing to Transfer Data Between Threads . . . . . . . . . . . . . . . . . . . . 355 Channels and Ownership Transference . . . . . . . . . . . . . . . . . . . . . . . . . . . 358 Sending Multiple Values and Seeing the Receiver Waiting . . . . . . . . . . . . . . 359 Creating Multiple Producers by Cloning the Transmitter . . . . . . . . . . . . . . . . 360 Shared-State Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361 Using Mutexes to Allow Access to Data from One Thread at a Time . . . . . . . 362 Similarities Between RefCell<T>/Rc<T> and Mutex<T>/Arc<T> . . . . . . . . . . 368 Extensible Concurrency with the Sync and Send Traits . . . . . . . . . . . . . . . . . . . . . . . 368 Allowing Transference of Ownership Between Threads with Send . . . . . . . . . 369 Allowing Access from Multiple Threads with Sync . . . . . . . . . . . . . . . . . . . . 369 Implementing Send and Sync Manually Is Unsafe . . . . . . . . . . . . . . . . . . . . 369 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Page 18
xvi Contents in Detail 17 OBJECT-ORIENTED PROGRAMMING FEATURES OF RUST 371 Characteristics of Object-Oriented Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371 Objects Contain Data and Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372 Encapsulation That Hides Implementation Details . . . . . . . . . . . . . . . . . . . . 372 Inheritance as a Type System and as Code Sharing . . . . . . . . . . . . . . . . . . 374 Using Trait Objects That Allow for Values of Different Types . . . . . . . . . . . . . . . . . . . 375 Defining a Trait for Common Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375 Implementing the Trait . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377 Trait Objects Perform Dynamic Dispatch . . . . . . . . . . . . . . . . . . . . . . . . . . 380 Object Safety Is Required for Trait Objects . . . . . . . . . . . . . . . . . . . . . . . . . 380 Implementing an Object-Oriented Design Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . 382 Defining Post and Creating a New Instance in the Draft State . . . . . . . . . . . . 383 Storing the Text of the Post Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384 Ensuring the Content of a Draft Post Is Empty . . . . . . . . . . . . . . . . . . . . . . . 384 Requesting a Review of the Post Changes Its State. . . . . . . . . . . . . . . . . . . . 385 Adding the approve Method that Changes the Behavior of content . . . . . . . . 386 Trade-offs of the State Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393 18 PATTERNS AND MATCHING 395 All the Places Patterns Can Be Used . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396 match Arms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396 Conditional if let Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396 while let Conditional Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398 for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398 let Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399 Function Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400 Refutability: Whether a Pattern Might Fail to Match . . . . . . . . . . . . . . . . . . . . . . . . . 401 Pattern Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402 Matching Literals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402 Matching Named Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403 Multiple Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404 Matching Ranges of Values with the ... Syntax . . . . . . . . . . . . . . . . . . . . . . 404 Destructuring to Break Apart Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405 Ignoring Values in a Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409 Extra Conditionals with Match Guards. . . . . . . . . . . . . . . . . . . . . . . . . . . . 413 @ Bindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416 19 ADVANCED FEATURES 417 Unsafe Rust . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418 Unsafe Superpowers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418 Dereferencing a Raw Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419 Calling an Unsafe Function or Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . 421 Accessing or Modifying a Mutable Static Variable . . . . . . . . . . . . . . . . . . . 425 Implementing an Unsafe Trait . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426 When to Use Unsafe Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Page 19
Contents in Detail xvii Advanced Traits. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427 Specifying Placeholder Types in Trait Definitions with Associated Types . . . . . 427 Default Generic Type Parameters and Operator Overloading . . . . . . . . . . . . 429 Fully Qualified Syntax for Disambiguation: Calling Methods with the Same Name . . . . . . . . . . . . . . . . . . . . . . . . 431 Using Supertraits to Require One Trait’s Functionality Within Another Trait . . . 434 Using the Newtype Pattern to Implement External Traits on External Types . . . 436 Advanced Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437 Using the Newtype Pattern for Type Safety and Abstraction . . . . . . . . . . . . . 437 Creating Type Synonyms with Type Aliases . . . . . . . . . . . . . . . . . . . . . . . . 438 The Never Type That Never Returns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440 Dynamically Sized Types and the Sized Trait . . . . . . . . . . . . . . . . . . . . . . . 441 Advanced Functions and Closures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443 Function Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443 Returning Closures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445 Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446 The Difference Between Macros and Functions . . . . . . . . . . . . . . . . . . . . . . 446 Declarative Macros with macro_rules! for General Metaprogramming. . . . . . 446 Procedural Macros for Generating Code from Attributes . . . . . . . . . . . . . . . 449 How to Write a Custom derive Macro . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449 Attribute-like macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454 Function-like macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455 20 FINAL PROJECT: BUILDING A MULTITHREADED WEB SERVER 457 Building a Single-Threaded Web Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458 Listening to the TCP Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458 Reading the Request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460 A Closer Look at an HTTP Request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462 Writing a Response. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463 Returning Real HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464 Validating the Request and Selectively Responding . . . . . . . . . . . . . . . . . . . 465 A Touch of Refactoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466 Turning Our Single-Threaded Server into a Multithreaded Server . . . . . . . . . . . . . . . . 468 Simulating a Slow Request in the Current Server Implementation . . . . . . . . . . 468 Improving Throughput with a Thread Pool . . . . . . . . . . . . . . . . . . . . . . . . . 469 Graceful Shutdown and Cleanup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487 Implementing the Drop Trait on ThreadPool . . . . . . . . . . . . . . . . . . . . . . . . 487 Signaling to the Threads to Stop Listening for Jobs. . . . . . . . . . . . . . . . . . . . 489 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493 A KEYWORDS 495 Keywords Currently in Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495 Keywords Reserved for Future Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497 Raw Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
Page 20
xviii Contents in Detail B OPERATORS AND SYMBOLS 499 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499 Non-operator Symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501 C DERIVABLE TRAITS 507 Debug for Programmer Output. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508 PartialEq and Eq for Equality Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508 PartialOrd and Ord for Ordering Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509 Clone and Copy for Duplicating Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509 Hash for Mapping a Value to a Value of Fixed Size. . . . . . . . . . . . . . . . . . . . . . . . . 510 Default for Default Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510 D USEFUL DEVELOPMENT TOOLS 511 Automatic Formatting with rustfmt. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511 Fix Your Code with rustfix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512 More Lints with Clippy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513 IDE Integration Using the Rust Language Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514 E EDITIONS 515 INDEX 517
The above is a preview of the first 20 pages. Register to read the complete e-book.

Recommended for You

Loading recommended books...
Failed to load, please try again later

Tip the Site

Scan the WeChat Pay or Alipay code to tip. No login required.

WeChat Pay
Alipay
← Back to List