100% found this document useful (1 vote)
13 views52 pages

The Rust Programming Language 2nd Edition Steve Klabnik instant download

The document provides information about the 2nd edition of 'The Rust Programming Language' by Steve Klabnik and Carol Nichols, including links to download the book and other related Rust programming resources. It contains details about the authors, technical reviewers, and the book's structure, including chapters covering various programming concepts and features in Rust. Additionally, it includes copyright information and customer service contacts.

Uploaded by

aoyspkvhlz909
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
13 views52 pages

The Rust Programming Language 2nd Edition Steve Klabnik instant download

The document provides information about the 2nd edition of 'The Rust Programming Language' by Steve Klabnik and Carol Nichols, including links to download the book and other related Rust programming resources. It contains details about the authors, technical reviewers, and the book's structure, including chapters covering various programming concepts and features in Rust. Additionally, it includes copyright information and customer service contacts.

Uploaded by

aoyspkvhlz909
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

The Rust Programming Language 2nd Edition Steve

Klabnik download

https://textbookfull.com/product/the-rust-programming-
language-2nd-edition-steve-klabnik/

Download full version ebook from https://textbookfull.com


We believe these products will be a great fit for you. Click
the link to download now, or visit textbookfull.com
to discover even more!

The Rust Programming Language Covers Rust 2018 Steve


Klabnik

https://textbookfull.com/product/the-rust-programming-language-
covers-rust-2018-steve-klabnik/

The Rust Programming Language 1st Edition Steve Klabnik

https://textbookfull.com/product/the-rust-programming-
language-1st-edition-steve-klabnik/

The Rust Programming Language 2nd Edition Steve Klabnik


And Carol Nichols

https://textbookfull.com/product/the-rust-programming-
language-2nd-edition-steve-klabnik-and-carol-nichols/

Programming Rust, 2nd Edition (Early Release) Jim


Blandy

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/

The Complete Rust Programming Reference Guide Rahul


Sharma

https://textbookfull.com/product/the-complete-rust-programming-
reference-guide-rahul-sharma/

Programming WebAssembly with Rust 1st Edition Kevin


Hoffman

https://textbookfull.com/product/programming-webassembly-with-
rust-1st-edition-kevin-hoffman/

Network Programming With Rust 1st Edition Abhishek


Chanda

https://textbookfull.com/product/network-programming-with-
rust-1st-edition-abhishek-chanda/

Asynchronous Programming in Rust 1st Edition Carl


Fredrik Samson

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

ISBN-13: 978-1-7185-0310-6 (print)


ISBN-13: 978-1-7185-0311-3 (ebook)

Publisher: William Pollock


Managing Editor: Jill Franklin
Production Manager: Sabrina Plomitallo-González
Production Editors: Jennifer Kepler and Katrina Horlbeck Olsen
Developmental Editor: Liz Chadwick
Cover Illustration: Karen Rustad Tölva
Interior Design: Octopod Studios
Technical Reviewer: JT
Copyeditor: Audrey Doyle
Compositor: Jeff Lytle, Happenstance Type-O-Rama
Proofreader: Liz Wheeler
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

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.

About the Technical Reviewer


JT is a Rust core team member and the co-creator of the Rust error message
format, Rust Language Server (RLS), and Nushell. They first started using
Rust in 2011, and in 2016 joined Mozilla to work on Rust full-time, helping
to shape its direction for widespread use. These days, they are a freelance
Rust trainer and advocate for safe systems programming.
BRIEF CONTENTS

Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

Chapter 6: Enums and Pattern Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

Chapter 7: Managing Growing Projects with Packages, Crates, and Modules . . . . . . . . . . 119

Chapter 8: Common Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141

Chapter 9: Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161

Chapter 10: Generic Types, Traits, and Lifetimes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181

Chapter 11: Writing Automated Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215

Chapter 12: An I/O Project: Building a Command Line Program . . . . . . . . . . . . . . . . . . . 243

Chapter 13: Functional Language Features: Iterators and Closures . . . . . . . . . . . . . . . . . . 273

Chapter 14: More About Cargo and Crates.io . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295

Chapter 15: Smart Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315

Chapter 16: Fearless Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353

Chapter 17: Object-Oriented Programming Features . . . . . . . . . . . . . . . . . . . . . . . . . . . 375

Chapter 18: Patterns and Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397


Chapter 19: Advanced Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419

Chapter 20: Final Project: Building a Multithreaded Web Server . . . . . . . . . . . . . . . . . . . 459

Appendix A: Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495

Appendix B: Operators and Symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499

Appendix C: Derivable Traits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507

Appendix D: Useful Development Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511

Appendix E: Editions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517

viii Brief Contents


CO N T E N T S I N D E TA I L

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

xii Contents in Detail


Traits as Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Returning Types That Implement Traits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Using Trait Bounds to Conditionally Implement Methods . . . . . . . . . . . . . . . . 200
Validating References with Lifetimes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Preventing Dangling References with Lifetimes . . . . . . . . . . . . . . . . . . . . . . . 201
The Borrow Checker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Generic Lifetimes in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Lifetime Annotation Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Lifetime Annotations in Function Signatures . . . . . . . . . . . . . . . . . . . . . . . . . 205
Thinking in Terms of Lifetimes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Lifetime Annotations in Struct Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Lifetime Elision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Lifetime Annotations in Method Definitions . . . . . . . . . . . . . . . . . . . . . . . . . 212
The Static Lifetime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Generic Type Parameters, Trait Bounds, and Lifetimes Together . . . . . . . . . . . . . . . . . . 213
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213

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

Contents in Detail xiii


Working with Environment Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Writing a Failing Test for the Case-Insensitive Search Function . . . . . . . . . . . . 265
Implementing the search_case_insensitive Function . . . . . . . . . . . . . . . . . . . . 266
Writing Error Messages to Standard Error Instead of Standard Output . . . . . . . . . . . . . 270
Checking Where Errors Are Written . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Printing Errors to Standard Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272

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

xiv Contents in Detail


Another Random Scribd Document
with Unrelated Content
APPENDIX B
THE SEA LIBRARY
The books an officer should have, and study, are appended.

owditch's American Practical Navigator.


The Navigator's Bible.

ecky's Wrinkles in Practical Navigation.


No comment needed. An officer who does not know this book—well,
he is simply not all there as an officer.

radford's Whys and Wherefores of Navigation.


An American book, of intimate and interesting information on the
subject of navigation. If you want to know "why," own and study
Bradford, it is a book for the man who likes to thoroughly understand
his subject—simple, clear, complete.

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.

odd and Whall's Seamanship.


The best book to date for the merchantman.

Walton's Know Your Own Ship.


Information about vessels that every officer aboard ship should
possess.

White's Oil Tank Steamers.


An excellent work by an officer who knows his subject.

llingham's Weather Signs and How to Read Them.


Index
Page numbers in bold-face type indicate reference to the Navigation
Laws.
A
Abandonment of seamen, 297
Able seamen, duties, 236-240
Advance, 265
Agreement, form of, 241-242
in coasting trade, 256
in foreign trade, 249, 250
Allingham, 319
Allotment of wages, 265, 266, 267
Alongside, 96
Amendments to Rule V, 155
Amplitudes, 181
Anchor watch, 74
Anchorage data, 73
Anchoring, 95
Application for register, 9
Apprentices, 244, 248, 249
Arbitration, 270
Arms, export to American countries, 61
sale of, 62
Arson, 295
Assault, 293
Assistance at sea, 62, 63
Average, general, 51, 52
Awnings, 77

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.

All Our Inquiries are Cheerfully and Carefully


Answered and Complete Catalogs as well as
Special Lists are Sent Free on Request.
D. VAN NOSTRAND COMPANY
Publishers and Booksellers
25 PARK PLACE NEW YORK
VAN NOSTRAND'S NAUTICAL MANUALS
175 Pages 5 × 7½ Postpaid, $2.00

The Whys and Wherefores


of
Navigation
By
GERSHOM BRADFORD, 2d.
Navigating Officer and Senior Instructor
New York State Nautical Schoolship Newport
Late Nautical Expert
Hydrographic Office, U.S.N.

Contents: Nautical Astronomy. Declination.


Right Ascension. Precession of the Equinoxes.
Time. Sidereal Time. Equation of Time.
Calendar. Corrections for Observed Altitude.
Index Correction. Semi-diameter. Refraction.
Dip. Parallax. Circum or Ex-Meridian Altitude.
Polaris. Observations for the Correction of the
Compass. Longitude. Sumner Method. The New
Navigation. The Moon. Charts.
An American book of intimate and interesting
information on the subject of Navigation. If you
want to know "why," own and study Bradford. It
is a book for the man who likes to understand
his subject thoroughly—simple, clear, complete.

VAN NOSTRAND'S MILITARY BOOKS


231 Pages 51 Plates—Many in Colors Pocket Size Price
$1.00

MILITARY and NAVAL


RECOGNITION BOOK
A handbook on the organization, insignia of rank, and
customs of the service of the World's Important Armies
and Navies.
BY
Lieut. J. W. BUNKLEY
U. S. Navy

A complete, correct and fully illustrated guide


to the insignia of our army and navy as well as
of those of the nations engaged in the war,
compiled from official sources and useful alike
to civilians as well as to the Army and Navy.
You will need this book to help you recognize
our boys at home and to distinguish the other
boys "over there."
D. VAN NOSTRAND COMPANY
HEADQUARTERS SINCE 1860
for
MILITARY AND NAVAL BOOKS
25 Park Place NEW YORK

VAN NOSTRAND'S NAVAL BOOKS


730 Pages 6¼ × 9¼ Postpaid $6.50
159 Full Page Plates 10 Color Plates

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.

VAN NOSTRAND'S NAUTICAL MANUALS


150 Illustrations 5 × 7½ about 400 pp. In Press

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.

Creating the works from print editions not protected by U.S.


copyright law means that no one owns a United States copyright in
these works, so the Foundation (and you!) can copy and distribute it
in the United States without permission and without paying
copyright royalties. Special rules, set forth in the General Terms of
Use part of this license, apply to copying and distributing Project
Gutenberg™ electronic works to protect the PROJECT GUTENBERG™
concept and trademark. Project Gutenberg is a registered trademark,
and may not be used if you charge for an eBook, except by following
the terms of the trademark license, including paying royalties for use
of the Project Gutenberg trademark. If you do not charge anything
for copies of this eBook, complying with the trademark license is
very easy. You may use this eBook for nearly any purpose such as
creation of derivative works, reports, performances and research.
Project Gutenberg eBooks may be modified and printed and given
away—you may do practically ANYTHING in the United States with
eBooks not protected by U.S. copyright law. Redistribution is subject
to the trademark license, especially commercial redistribution.

START: FULL LICENSE


THE FULL PROJECT GUTENBERG LICENSE
PLEASE READ THIS BEFORE YOU DISTRIBUTE OR USE THIS WORK

To protect the Project Gutenberg™ mission of promoting the free


distribution of electronic works, by using or distributing this work (or
any other work associated in any way with the phrase “Project
Gutenberg”), you agree to comply with all the terms of the Full
Project Gutenberg™ License available with this file or online at
www.gutenberg.org/license.

Section 1. General Terms of Use and


Redistributing Project Gutenberg™
electronic works
1.A. By reading or using any part of this Project Gutenberg™
electronic work, you indicate that you have read, understand, agree
to and accept all the terms of this license and intellectual property
(trademark/copyright) agreement. If you do not agree to abide by all
the terms of this agreement, you must cease using and return or
destroy all copies of Project Gutenberg™ electronic works in your
possession. If you paid a fee for obtaining a copy of or access to a
Project Gutenberg™ electronic work and you do not agree to be
bound by the terms of this agreement, you may obtain a refund
from the person or entity to whom you paid the fee as set forth in
paragraph 1.E.8.

1.B. “Project Gutenberg” is a registered trademark. It may only be


used on or associated in any way with an electronic work by people
who agree to be bound by the terms of this agreement. There are a
few things that you can do with most Project Gutenberg™ electronic
works even without complying with the full terms of this agreement.
See paragraph 1.C below. There are a lot of things you can do with
Project Gutenberg™ electronic works if you follow the terms of this
agreement and help preserve free future access to Project
Gutenberg™ electronic works. See paragraph 1.E below.
1.C. The Project Gutenberg Literary Archive Foundation (“the
Foundation” or PGLAF), owns a compilation copyright in the
collection of Project Gutenberg™ electronic works. Nearly all the
individual works in the collection are in the public domain in the
United States. If an individual work is unprotected by copyright law
in the United States and you are located in the United States, we do
not claim a right to prevent you from copying, distributing,
performing, displaying or creating derivative works based on the
work as long as all references to Project Gutenberg are removed. Of
course, we hope that you will support the Project Gutenberg™
mission of promoting free access to electronic works by freely
sharing Project Gutenberg™ works in compliance with the terms of
this agreement for keeping the Project Gutenberg™ name associated
with the work. You can easily comply with the terms of this
agreement by keeping this work in the same format with its attached
full Project Gutenberg™ License when you share it without charge
with others.

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. Unless you have removed all references to Project Gutenberg:

1.E.1. The following sentence, with active links to, or other


immediate access to, the full Project Gutenberg™ License must
appear prominently whenever any copy of a Project Gutenberg™
work (any work on which the phrase “Project Gutenberg” appears,
or with which the phrase “Project Gutenberg” is associated) is
accessed, displayed, performed, viewed, copied or distributed:
This eBook is for the use of anyone anywhere in the United
States and most other parts of the world at no cost and with
almost no restrictions whatsoever. You may copy it, give it away
or re-use it under the terms of the Project Gutenberg License
included with this eBook or online at www.gutenberg.org. If you
are not located in the United States, you will have to check the
laws of the country where you are located before using this
eBook.

1.E.2. If an individual Project Gutenberg™ electronic work is derived


from texts not protected by U.S. copyright law (does not contain a
notice indicating that it is posted with permission of the copyright
holder), the work can be copied and distributed to anyone in the
United States without paying any fees or charges. If you are
redistributing or providing access to a work with the phrase “Project
Gutenberg” associated with or appearing on the work, you must
comply either with the requirements of paragraphs 1.E.1 through
1.E.7 or obtain permission for the use of the work and the Project
Gutenberg™ trademark as set forth in paragraphs 1.E.8 or 1.E.9.

1.E.3. If an individual Project Gutenberg™ electronic work is posted


with the permission of the copyright holder, your use and distribution
must comply with both paragraphs 1.E.1 through 1.E.7 and any
additional terms imposed by the copyright holder. Additional terms
will be linked to the Project Gutenberg™ License for all works posted
with the permission of the copyright holder found at the beginning
of this work.

1.E.4. Do not unlink or detach or remove the full Project


Gutenberg™ License terms from this work, or any files containing a
part of this work or any other work associated with Project
Gutenberg™.

1.E.5. Do not copy, display, perform, distribute or redistribute this


electronic work, or any part of this electronic work, without
prominently displaying the sentence set forth in paragraph 1.E.1
with active links or immediate access to the full terms of the Project
Gutenberg™ License.

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.

1.E.7. Do not charge a fee for access to, viewing, displaying,


performing, copying or distributing any Project Gutenberg™ works
unless you comply with paragraph 1.E.8 or 1.E.9.

1.E.8. You may charge a reasonable fee for copies of or providing


access to or distributing Project Gutenberg™ electronic works
provided that:

• 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 provide a full refund of any money paid by a user who


notifies you in writing (or by e-mail) within 30 days of receipt
that s/he does not agree to the terms of the full Project
Gutenberg™ License. You must require such a user to return or
destroy all copies of the works possessed in a physical medium
and discontinue all use of and all access to other copies of
Project Gutenberg™ works.

• You provide, in accordance with paragraph 1.F.3, a full refund


of any money paid for a work or a replacement copy, if a
defect in the electronic work is discovered and reported to you
within 90 days of receipt of the work.

• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.

1.E.9. If you wish to charge a fee or distribute a Project Gutenberg™


electronic work or group of works on different terms than are set
forth in this agreement, you must obtain permission in writing from
the Project Gutenberg Literary Archive Foundation, the manager of
the Project Gutenberg™ trademark. Contact the Foundation as set
forth in Section 3 below.

1.F.

1.F.1. Project Gutenberg volunteers and employees expend


considerable effort to identify, do copyright research on, transcribe
and proofread works not protected by U.S. copyright law in creating
the Project Gutenberg™ collection. Despite these efforts, Project
Gutenberg™ electronic works, and the medium on which they may
be stored, may contain “Defects,” such as, but not limited to,
incomplete, inaccurate or corrupt data, transcription errors, a
copyright or other intellectual property infringement, a defective or
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

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.

Let us accompany you on the journey of exploring knowledge and


personal growth!

textbookfull.com

You might also like