The Rust Programming Language 2nd Edition Steve Klabnik instant download
The Rust Programming Language 2nd Edition Steve Klabnik instant download
Klabnik download
https://textbookfull.com/product/the-rust-programming-
language-2nd-edition-steve-klabnik/
https://textbookfull.com/product/the-rust-programming-language-
covers-rust-2018-steve-klabnik/
https://textbookfull.com/product/the-rust-programming-
language-1st-edition-steve-klabnik/
https://textbookfull.com/product/the-rust-programming-
language-2nd-edition-steve-klabnik-and-carol-nichols/
https://textbookfull.com/product/programming-rust-2nd-edition-
early-release-jim-blandy/
Programming Rust 1st Edition Jim Blandy
https://textbookfull.com/product/programming-rust-1st-edition-
jim-blandy/
https://textbookfull.com/product/the-complete-rust-programming-
reference-guide-rahul-sharma/
https://textbookfull.com/product/programming-webassembly-with-
rust-1st-edition-kevin-hoffman/
https://textbookfull.com/product/network-programming-with-
rust-1st-edition-abhishek-chanda/
https://textbookfull.com/product/asynchronous-programming-in-
rust-1st-edition-carl-fredrik-samson/
THE RUST PROGRAMMING LANGUAGE
THE RUST
PROGRAMMING
LANGUAGE
2nd Edition
by S t e v e K l a b n i k a n d C a r o l N i c h o ls ,
w it h con t r i b u t i o n s f r o m
t h e Ru s t C o m m u n i t y
San Francisco
THE RUST PROGRAMMING LANGUAGE, 2ND EDITION. Copyright © 2023 by the Rust
Foundation 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.
Third printing
27 26 25 24 23 34567
For customer service inquiries, please contact [email protected]. For information on distribution,
bulk sales, corporate sales, or translations: [email protected]. For permission to translate this work:
[email protected]. To report counterfeit copies or piracy: [email protected].
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 was the lead for the Rust documentation team and was one
of Rust’s core developers. A frequent speaker and a prolific open source con-
tributor, he previously worked on projects such as Ruby and Ruby on Rails.
Carol Nichols is a member of the Rust Crates.io Team and a former mem-
ber of the Rust Core Team. She’s a co-founder of Integer 32, LLC, the
world’s first Rust-focused software consultancy. Nichols has also organized
the Rust Belt Rust Conference.
Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
Chapter 7: Managing Growing Projects with Packages, Crates, and Modules . . . . . . . . . . 119
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517
FOREWORD xix
PREFACE xxi
ACKNOWLEDGMENTS xxiii
INTRODUCTION xxv
Who Rust Is For . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvi
Teams of Developers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvi
Students . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvi
Companies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvi
Open Source Developers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii
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
Installing rustup on Linux or macOS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Installing rustup on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Updating and Uninstalling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Cargo as Convention . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2
PROGRAMMING A GUESSING GAME 13
Setting Up a New Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Processing a Guess . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Storing Values with Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Receiving User Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Handling Potential Failure with Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Printing Values with println! Placeholders . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Testing the First Part . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Generating a Secret Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Using a Crate to Get More Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Generating a Random Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Comparing the Guess to the Secret Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Allowing Multiple Guesses with Looping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Quitting After a Correct Guess . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Handling Invalid Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3
COMMON PROGRAMMING CONCEPTS 31
Variables and Mutability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Shadowing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Scalar Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Compound Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Statements and Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Functions with Return Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Control Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
if Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Repetition with Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
References and Borrowing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Mutable References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Dangling References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
The Rules of References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
The Slice Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
String Slices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Other Slices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
x Contents in Detail
5
USING STRUCTS TO STRUCTURE RELATED DATA 85
Defining and Instantiating Structs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Using the Field Init Shorthand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Creating Instances from Other Instances with Struct Update Syntax . . . . . . . . . 88
Using Tuple Structs Without Named Fields to Create Different Types . . . . . . . . . 89
Unit-Like Structs Without Any Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
An Example Program Using Structs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Refactoring with Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Refactoring with Structs: Adding More Meaning . . . . . . . . . . . . . . . . . . . . . . 93
Adding Useful Functionality with Derived Traits . . . . . . . . . . . . . . . . . . . . . . . 94
Method Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Defining Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Methods with More Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Associated Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Multiple impl Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
6
ENUMS AND PATTERN MATCHING 103
Defining an Enum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Enum Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
The Option Enum and Its Advantages Over Null Values . . . . . . . . . . . . . . . . 108
The match Control Flow Construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Patterns That Bind to Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Matching with Option<T> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Matches Are Exhaustive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Catch-All Patterns and the _ Placeholder . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Concise Control Flow with if let . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
7
MANAGING GROWING PROJECTS
WITH PACKAGES, CRATES, AND MODULES 119
Packages and Crates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Defining Modules to Control Scope and Privacy . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Paths for Referring to an Item in the Module Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Exposing Paths with the pub Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Starting Relative Paths with super . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Making Structs and Enums Public . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Bringing Paths into Scope with the use Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Creating Idiomatic use Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Providing New Names with the as Keyword . . . . . . . . . . . . . . . . . . . . . . . . 135
Re-exporting Names with pub use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Using External Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Using Nested Paths to Clean Up Large use Lists . . . . . . . . . . . . . . . . . . . . . . 137
The Glob Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Separating Modules into Different Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Contents in Detail xi
8
COMMON COLLECTIONS 141
Storing Lists of Values with Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Creating a New Vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Updating a Vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Reading Elements of Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Iterating Over the Values in a Vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Using an Enum to Store Multiple Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Dropping a Vector Drops Its Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Storing UTF-8 Encoded Text with Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
What Is a String? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Creating a New String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Updating a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Indexing into Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Slicing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Methods for Iterating Over Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Strings Are Not So Simple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Storing Keys with Associated Values in Hash Maps . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Creating a New Hash Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Accessing Values in a Hash Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Hash Maps and Ownership . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Updating a Hash Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Hashing Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
9
ERROR HANDLING 161
Unrecoverable Errors with panic! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Recoverable Errors with Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Matching on Different Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Propagating Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
To panic! or Not to panic! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Examples, Prototype Code, and Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Cases in Which You Have More Information Than the Compiler . . . . . . . . . . 176
Guidelines for Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Creating Custom Types for Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
10
GENERIC TYPES, TRAITS, AND LIFETIMES 181
Removing Duplication by Extracting a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
Generic Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
In Function Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
In Struct Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
In Enum Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
In Method Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Performance of Code Using Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Traits: Defining Shared Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Defining a Trait . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Implementing a Trait on a Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Default Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
11
WRITING AUTOMATED TESTS 215
How to Write Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
The Anatomy of a Test Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Checking Results with the assert! Macro . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Testing Equality with the assert_eq! and assert_ne! Macros . . . . . . . . . . . . . . 222
Adding Custom Failure Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Checking for Panics with should_panic . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Using Result<T, E> in Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
Controlling How Tests Are Run . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
Running Tests in Parallel or Consecutively . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Showing Function Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Running a Subset of Tests by Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Ignoring Some Tests Unless Specifically Requested . . . . . . . . . . . . . . . . . . . . 235
Test Organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Integration Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
12
AN I/O PROJECT: BUILDING A COMMAND LINE PROGRAM 243
Accepting Command Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Reading the Argument Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Saving the Argument Values in Variables . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Reading a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Refactoring to Improve Modularity and Error Handling . . . . . . . . . . . . . . . . . . . . . . . 248
Separation of Concerns for Binary Projects . . . . . . . . . . . . . . . . . . . . . . . . . 249
Fixing the Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Extracting Logic from main . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Splitting Code into a Library Crate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Developing the Library’s Functionality with Test-Driven Development . . . . . . . . . . . . . . 259
Writing a Failing Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
Writing Code to Pass the Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
13
FUNCTIONAL LANGUAGE FEATURES:
ITERATORS AND CLOSURES 273
Closures: Anonymous Functions That Capture Their Environment . . . . . . . . . . . . . . . . . 274
Capturing the Environment with Closures . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Closure Type Inference and Annotation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Capturing References or Moving Ownership . . . . . . . . . . . . . . . . . . . . . . . . 278
Moving Captured Values Out of Closures and the Fn Traits . . . . . . . . . . . . . . 280
Processing a Series of Items with Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
The Iterator Trait and the next Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Methods That Consume the Iterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Methods That Produce Other Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Using Closures That Capture Their Environment . . . . . . . . . . . . . . . . . . . . . . 287
Improving Our I/O Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Removing a clone Using an Iterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Making Code Clearer with Iterator Adapters . . . . . . . . . . . . . . . . . . . . . . . . 292
Choosing Between Loops and Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
Comparing Performance: Loops vs. Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
14
MORE ABOUT CARGO AND CRATES.IO 295
Customizing Builds with Release Profiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
Publishing a Crate to Crates.io . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Making Useful Documentation Comments . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Exporting a Convenient Public API with pub use . . . . . . . . . . . . . . . . . . . . . . 300
Setting Up a Crates.io Account . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
Adding Metadata to a New Crate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
Publishing to Crates.io . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Publishing a New Version of an Existing Crate . . . . . . . . . . . . . . . . . . . . . . . 306
Deprecating Versions from Crates.io with cargo yank . . . . . . . . . . . . . . . . . . 306
Cargo Workspaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Creating a Workspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Creating the Second Package in the Workspace . . . . . . . . . . . . . . . . . . . . . 308
Installing Binaries with cargo install . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
Extending Cargo with Custom Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
15
SMART POINTERS 315
Using Box<T> to Point to Data on the Heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
Using Box<T> to Store Data on the Heap . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Enabling Recursive Types with Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
acoby's Navigation.
A good book for the beginner.
night's Seamanship.
The best American work on seamanship, naval and merchant
service. The chapters on ship handling and the higher problems of
seamanship are exceptionally good.
B
Baggage, 12
Barratry, 298
Berth, fair or foul, 75
Bills of lading, 75, 84
Boarding, 24, 25, 26
Boat covers, 77
falls, 108
handling, 105
Boatswain, 78
duties, 234, 235
Boatswain's stores, 77
Bond, general libel, 57, 58
Books, 319
Bowditch, 319
Bradford, 319
Bribery, 50, 51
Bridge, persons allowed on, 170
Bullion and coin, 21
Buoys, 111
C
Cadets, laws, duties, 145
Captain, call, 179
Cargo, battens, 79
care of, 82
chilled beef, 87
comparison of cargo and manifest, 39
clusters, 74, 75
coal-trimming, 85
coal-ventilation, 85
cotton, 86
damage to, 83
delivery in various districts, 33, 34
diagrams, 75, 84
duty to, 82
explosive, 65, 66, 67, 68, 69, 70, 71
frozen, 87
gear, 75, 80
general, 87, 88
grain, 86
in bulk, 31
inflammable, 65, 66, 67, 68, 69, 70, 71
kinds, 84
munitions, 89
ore, 85
pilfering, 83
re-export, 31
returns of unlading, 47, 48
special, 88
stowage, 83
ventilation, 84
wet coal, 85
Carpenter, 73, 78
duties, 231-232, 233
stores, 77
Cask fenders, 74
Casting away vessels, 299
Ceiling, 79
Chafe, 74
Chief Mate, the, 72
coastwise, 162
duties, 73, 74, 75
duties at sea, 100, 101, 102
duties in port, 78
examination, 161
License, 149
qualifications, 72
reports for duty, 72
service, 159
Vessel in stream, 73
Charts, pilot, 181
Clearance, form of, 20
Clothing, exempt from attachment, 267
Coal, 35
Coaling, 97
gear, 77
Cork fenders, 74
Cost data, 89
Collision, 175
Coming alongside, 96
to anchor, 95
Complaints re provisions, 12
Construction, future, 105
Conventions, 269
Corporal punishment, 290
Crew, 76
failure to produce, 253
ill treatment of, 297
papers relating to, 254
quarters, inspection, 75
Crimes, 291-300
D
Dangerous conditions, 176
Davits, test of, 106, 107
Death from negligence, 169
negligence, misconduct, 294
Deck, 178
crew, 78
engineer, 73
room, 115
Delivery, ports of, 29, App. A
Desertion, abroad, 268
of foreign seamen in U. S., 268
Discharge, 245
Discharging, 75
Discharge in foreign ports, 258
in foreign trade, 257
Discipline at sea, 286-287
Distress, vessels in, 48, 49
Docking, notes on, 99, 100
Docking plan, 98
Dodgers, 77
Draft, 75
Drift lead, 74
Dry dock, entering, 98
blue prints, 98
E
Engagement, period of, 251
Engineer's license, 150
Entry and clearance, 18
of merchandise, 27, 38
Ports of, 28, App. A
post, 47
Examinations, Chief and second mate coastwise, 164
master, 156
original license, 153
Third mate coastwise, 165
Exemption, militia duty, 241
of private property at sea, 62
Export of arms, 61
merchandise laden for, 32
transfer of imported merchandise, 32
F
Fees, 21
Fellow-servant clause, 283
Fire-fighting equipment, 92
Fire lines, 75
Fire, protection against, 63, 64, 65
Flogging, 290
Forgery, 296
Freeboard, 83
G
Gangway, 74
General average, 51-52
General libel bond, 57, 58
Great Lakes, licenses, 166
crimes on, 300
Ground tackle, 94
Gun, drill with line carrying, 113
line carrying, 112
tests, 112
H
Harbor defenses, 61
regulations, 92
Harter Act, 55, 56, 57
Hatch battens, 76
covers, 76
Hawsers, 77
Heaving in, 95
lines, 74
Holds, 75
preparation to receive cargo, 78, 79, 80
Hose, on foc's'le, 73
I
Ice, 176
obstruction by, 49
Illegal boarding, 24
unlading, 39, 40
Inland licenses, 165
Rules of road, 185-222
waters—limits, 222-228
Inspection of inward manifests by boarding officers, 35, 36, 37, 38
Instructions for gun and rocket apparatus, 113
Insurance, war risk, 283, 284, 285
International Rules of Road, 184-220
Inventories, 76
Invoices, 84
J
Jacoby, navigation, 319
Junior officers, duties, 133
Jurisdiction over seamen, 277, 278, 279
K
Keys, 92
Keys, master, 92
Knight, 319
L
Larceny, 295
Laws effecting duties of master, 9
Lecky, 319
Log books, 15, 16, 17
Look out, 176
Liability of owners, masters, shippers, 53, 54, 55
Libel bond, general, 57, 58
Licenses, duration of, 147
renewal of, 154
Liens for freight or general average, 51, 52
Lifeboats, care of, 108
equipment, 103
how carried, 105
launching, 108
manning, 108-109
marking of, 107
men, certificated, 108-109
provisions, 104
Life buoys, ring, 111
preservers, 111
rafts, equipment, 110
care of, 110
manning, 109
marking, 110
saving equipment, 77, 94 105
Lighters, 73
Lights, running, 179
Live stock, 90
Lodgers, 270
Luminous buoys, 111
M
Mail, 129
Manifest, 18, 19, 20, 27, 28, 30, 31
Manning of merchant vessels, 280, 281, 282
Manslaughter, 292
Mast covers, 77
Master, the, 1
acting as pilot, 151
always in command, 4
articles, 2
authority over pilot, 4
authority to protect lives and property, 8
authority to suspend officers, 8
cannot delegate responsibility, 4
certificate of inspection, 7
change of, 9
charter, 4
coaling, 2
coastwise, 157
correct lading, 3
course of vessel, 7
declaration of, 29, 30
delivery of specie and cargo, 3
departure, 7
disaster, 8
drills, 7
duties, 2, 3, 4, 5, 6, 7, 8
enters and clears, 3
examination, 156
general management of vessel, 2
insurance, 3
keeps official log, 2
keeps wage account, 4
last to leave ship, 8
laws, must be familiar with, 3
of port, 5
liability, 53, 54, 55, 169
license, 149
licensed engineer officer on duty, 3
licensed officer always in charge, 4
live stock, 5
maneuvering vessel, 3
medicine chest, 3
navigator, 5
navigation laws, 8
oath-manifest, 18
oath of citizenship, 9
official log book, 5
passengers, 4, 7
property of deceased persons, 7
punishments to crew, 5
qualifications, 2
removal of, 152
report to U. S. Local Inspectors, 7
represents owner, 4
report on accidents, 7
responsibilities, 1, 2, 4
rules of road, 5
safe carrying of mail, 4
navigation, 2
sail, 158
scale of provisions, 3
service required, 155
ship's log book, 7
ship must be well found, 2
skill, 8
slop chest, 3
smuggling, 5
Mate, acting as pilot, 151
Mayhem, 294
Medicines and anti-scorbutics, 14
Merchandise, fraudulent importation, 50
salvage of, 49, 55
vessels, manning, 280, 282, 282
Mines, 61, 62
Miscellaneous laws, 59
Mooring lines, 73, 74
Murder, 292
Muster-list roll and drill, 114-115
Mutiny, 297
N
Navigation, 319
Navigating gear, 77
officer, 129-130
Navy ration, 60, 61
Night orders, 176, 177, 178
O
Oath of officer, 151
Offenses, 287, 288, 289
Offenses against registry law, 9
miscellaneous, 296
Officer's citizenship, 146
license, 148, 149
serving during war, 148
Orders, night, 178
standing, 177
Owner's liability, 53, 54, 55
may ship seamen, 248
P
Pacific Islanders, sale of arms to, 62
Painting, bottom, 99
Paints, 77
Papers, deposit of, 21
Passengers, 90
Passenger accommodations, 116, 117, 118, 119
Act of 1882, 116
vessel, boarding, 124
steamer, cattle on board, 123
carriage of, 126
cleanliness, 122
death of, 125
discipline, 122
embarkation in boats and rafts, 108
steamer, explosives on board, 123
inspection, 125
light and air, 119, 120
list, 124
medical attendance, 121, 122
act, penalties, 125-126
privacy, 122-123
provisions, 121
Passports, 22-23
Permit to unlade by night, 40, 41, 42
Pilot's license, 151
Piracy, 300, 301, 302, 303
Plundering vessel, 299
Pilot, must yield to master, 4
Ports of entry, 29, App. A
Post entry, 47
Provisions, scale of, 243
Provisions and water, 11
Private property, exemption at sea, 62
Procedure, 290
Projectiles, line carrying, 113
Propeller signs, 74
Protection against fire, 63, 64, 65
Punishments, 287, 288, 289
Q
Quartermasters, 78, 182
duties, 229-230
R
Rafts, handling, 105
Rape, 293
Rat guards, 75, 97
Ration, navy, 60, 61
Reduction of allowance, 13
Responsibility, watch officers, 172
Riding lights, 74
Robbery, 295
Rockets, 112
Rose boxes, 79
Rules of road, 173, 184-221
Rule, unwritten, 174
S
Sails, 77
Sale of arms to Pacific Islanders, 62
Salvage at sea, 62, 63
Salvage of merchandise, 49, 50
School ships, 59, 60
Sea letters, 22, 23
Sea library, 319
Seamen, abandonment of, 297
effects of deceased, 272-276
naturalization of, 245
Seamen, rescuing shipwrecked American, 59
return of, 271
shipment in coasting trade, 255
sick and disabled, 276, 277
witness fees, 279
Search, 24, 25, 26
Sea stores, 34, 35
Second mate, 78, 128, 129, 160, 161, 163
Seduction, 293
Service, substituting, 155
Sheath knives, 289
Shipment, before consul, 252
Shipment without agreement, penalty, 251
Shipments, illegal, 247
Shipper's liability, 53, 54, 55
Shipping commissioner, 270
Shipping officers, 246
Ship's register, 9
school, 59, 60
Shipwrecked American seamen rescuing, 59
Side ladders, 74
screens, 77
Signal gear, 77
Skids, 74
Slop chest, 14
Smoke helmets, 87
Smothering lines, 79
Solicitation of bribes, 50, 51
Soliciting lodgers, 270
Sounding pipes, 76
Stairways, 115
State inspection laws, 20
Station bill, 75, 76
Steamer's name on equipment, 114
Steel strongbacks, 76
Stevedores, 82
Stolen property, 295
Stores, receiving, 92
Storm oil, 113
Supervising Inspector General, Recommendations, 180
T
Tank steamers, 90-91
Tarpaulins, 76, 77
Telegraph, 73
Third mate, 78, 131, 132, 161, 162, 164
Todd and Whall, 319
Torpedoes, 61
Treaties, 269
Trial, place of, 291
Turning to, 92, 93
Twin screws, 74
U
Undermanning, 283
Unloading by day, 42, 43
by night, 40
supervision of, 43, 44, 45
time limit for, 46
Upkeep, 93
Unseaworthy vessels, 283
U. S. Local Inspectors, 7
Supervising Inspectors, rules relating to life saving, 103
V
Ventilator covers, 77
Vessel alongside, 74
casting away, 299
Vessels, different types, 76
exempt from entry, 34
in distress, 48, 49
Vessels, overtaken, 179
plundering, 299
unseaworthy, 283
W
Wages, 260-265
exempt from attachment, 267
Walton, 319
War documents, 22, 23
Warmth and clothing, 15
War risk insurance, 283, 284, 285
War zone, 179
precautions, 105
Waterline, striking, 100
Water plugs, 75
Watch officer, 167
duty, 167
in port, 183
not citizens, 147
Watch-relieving, 170
rest before going on, 169
Watches, three, 168
Wedges, 76
Weights and measures, 13
of persons, 107
Whistle signals, 81
White, 319
Windlass notes, 73
Wooden steamers, 86
warehouse, 74
Working boat, 74
cargo, 81
Work of Chief mate, 76
Wrecking, 298
Z
Zig-zag, 180
BY THE SAME AUTHOR
UNDER SAIL
A narrative of the old square-rigger sailing
ship days, detailing the story of a voyage
around Cape Horn to Honolulu and back to New
York in the American three skysail yarder A. J.
Fuller in 1897-8.
IN PRESS
THE LITERATURE of NAVAL AND
MARINE SCIENCE
On our shelves is the most complete stock of
technical, industrial, engineering and scientific
books in the United States. The technical
literature of every branch of naval architecture,
construction, ordnance, marine engineering,
seamanship and navigation is well represented,
as well as are the various other related
subjects.
A large number of these we publish and for
an ever increasing number we are the sole
agents.
Modern Seamanship
By
Admiral Austin M. Knight, U. S. N.
SEVENTH EDITION
REVISED AND ENLARGED
CONTENTS—The Hull and Fittings of a Ship. Rope.
Knotting and Splicing. Mechanical Appliances on
Shipboard. Blocks and Tackles. Handling Heavy Weights.
Compass. Log and Lead. Submarine Signals. Boats.
Handling Boats in a Surf. Ground Tackle. Carrying Out
Anchors. The Steering of Steamers. The Rules of the
Road. Manoeuvering to Avoid Collision. Piloting. Handling
a Steamer Alongside a Dock. Placing a Ship in a Dry
Dock. Weather and the Laws of Storms. Handling
Steamers in Heavy Weather. The Handling of Torpedo
Vessels. Keeping Stations and Manoeuvring in Squadron.
Towing. Rescuing the Crew of a Wreck. Man Overboard.
Stranding. Hints for Junior Officers Doing Line Duty.
Appendix.
The official text book at the U. S. Naval
Academy and in use throughout the service.
Describes the handling of all vessels, men-of-
war, merchantmen, naval auxiliaries and small
boats.
HANDBOOK
FOR THE CARE AND OPERATION OF
NAVAL MACHINERY
By Comm. H. C. DINGER, U. S. Navy
Third Edition, Revised and Enlarged
by Lieut. Comm. H. T. Dyer, U.S.N.
HIS new, revised and enlarged
edition will be of use to all
engineers and will help any one
that has to do with nautical
matters. The explanations and
definitions are so plain that save in a few cases
the layman can take up the book and by
reading the section he is interested in can
understand the point at once. Special care has
been exercised to cover all points dealing with
the usage on United States vessels. A glance at
the book will show the infinite number of things
that must be attended to by the engineer, who
nowadays is the main factor in the ship's
welfare.
D. VAN NOSTRAND COMPANY
Publishers and Booksellers
25 PARK PLACE NEW YORK
*** END OF THE PROJECT GUTENBERG EBOOK THE MEN ON DECK:
MASTER, MATES AND CREW, THEIR DUTIES AND
RESPONSIBILITIES ***
Updated editions will replace the previous one—the old editions will
be renamed.
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the
terms of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or
expense to the user, provide a copy, a means of exporting a copy, or
a means of obtaining a copy upon request, of the work in its original
“Plain Vanilla ASCII” or other form. Any alternate format must
include the full Project Gutenberg™ License as specified in
paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™
trademark, but he has agreed to donate royalties under this
paragraph to the Project Gutenberg Literary Archive
Foundation. Royalty payments must be paid within 60 days
following each date on which you prepare (or are legally
required to prepare) your periodic tax returns. Royalty
payments should be clearly marked as such and sent to the
Project Gutenberg Literary Archive Foundation at the address
specified in Section 4, “Information about donations to the
Project Gutenberg Literary Archive Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com