The Rust Programming Language, Second Edition Steve Klabnik
The Rust Programming Language, Second Edition Steve Klabnik The Rust Programming Language, Second Edition Steve Klabnik The Rust Programming Language, Second Edition Steve Klabnik
The Rust Programming Language, Second Edition Steve Klabnik
1.
Read Anytime AnywhereEasy Ebook Downloads at ebookmeta.com The Rust Programming Language, Second Edition Steve Klabnik https://ebookmeta.com/product/the-rust-programming-language- second-edition-steve-klabnik/ OR CLICK HERE DOWLOAD EBOOK Visit and Get More Ebook Downloads Instantly at https://ebookmeta.com
2.
Recommended digital products(PDF, EPUB, MOBI) that you can download immediately if you are interested. The Rust Programming Language Steve Klabnik https://ebookmeta.com/product/the-rust-programming-language-steve- klabnik/ ebookmeta.com The Rust Programming Language 2nd Edition Steve Klabnik https://ebookmeta.com/product/the-rust-programming-language-2nd- edition-steve-klabnik/ ebookmeta.com Black Hat Rust Applied offensive security with the Rust programming language Sylvain Kerkour https://ebookmeta.com/product/black-hat-rust-applied-offensive- security-with-the-rust-programming-language-sylvain-kerkour-2/ ebookmeta.com Introduction to Criminology LA2010 Module Guide University of London 2020th Edition S. Hutchinson https://ebookmeta.com/product/introduction-to-criminology- la2010-module-guide-university-of-london-2020th-edition-s-hutchinson/ ebookmeta.com
3.
Programmer Passport OTPBruce Tate https://ebookmeta.com/product/programmer-passport-otp-bruce-tate-2/ ebookmeta.com An Introduction to the French Poets Villon to the Present Day 3rd Edition Geoffrey Brereton https://ebookmeta.com/product/an-introduction-to-the-french-poets- villon-to-the-present-day-3rd-edition-geoffrey-brereton/ ebookmeta.com Shadow Isle Reformatory Box Set 1 4 1st Edition Everly Taylor Melody Calder https://ebookmeta.com/product/shadow-isle-reformatory-box-set-1-4-1st- edition-everly-taylor-melody-calder/ ebookmeta.com Consumer Behavior, 8e 8th Edition Wayne D. Hoyer https://ebookmeta.com/product/consumer-behavior-8e-8th-edition-wayne- d-hoyer/ ebookmeta.com After the Fall The Complete Collection 1st Edition Anya Merchant https://ebookmeta.com/product/after-the-fall-the-complete- collection-1st-edition-anya-merchant/ ebookmeta.com
4.
Network Defense andCountermeasures: Principles and Practices, 4th Edition William Easttom https://ebookmeta.com/product/network-defense-and-countermeasures- principles-and-practices-4th-edition-william-easttom/ ebookmeta.com
6.
CONTENTS IN DETAIL TITLEPAGE COPYRIGHT ABOUT THE AUTHORS FOREWORD PREFACE ACKNOWLEDGMENTS INTRODUCTION Who Rust Is For Teams of Developers Students Companies Open Source Developers People Who Value Speed and Stability Who This Book Is For How to Use This Book Resources and How to Contribute to This Book CHAPTER 1: GETTING STARTED Installation Installing rustup on Linux or macOS
7.
Installing rustup onWindows Troubleshooting Updating and Uninstalling Local Documentation Hello, World! Creating a Project Directory Writing and Running a Rust Program Anatomy of a Rust Program Compiling and Running Are Separate Steps Hello, Cargo! Creating a Project with Cargo Building and Running a Cargo Project Building for Release Cargo as Convention Summary CHAPTER 2: PROGRAMMING A GUESSING GAME Setting Up a New Project Processing a Guess Storing Values with Variables Receiving User Input Handling Potential Failure with Result Printing Values with println! Placeholders Testing the First Part Generating a Secret Number Using a Crate to Get More Functionality Generating a Random Number Comparing the Guess to the Secret Number Allowing Multiple Guesses with Looping Quitting After a Correct Guess
8.
Handling Invalid Input Summary CHAPTER3: COMMON PROGRAMMING CONCEPTS Variables and Mutability Constants Shadowing Data Types Scalar Types Compound Types Functions Parameters Statements and Expressions Functions with Return Values Comments Control Flow if Expressions Repetition with Loops Summary CHAPTER 4: UNDERSTANDING OWNERSHIP What Is Ownership? Ownership Rules Variable Scope The String Type Memory and Allocation Ownership and Functions Return Values and Scope References and Borrowing Mutable References
9.
Dangling References The Rulesof References The Slice Type String Slices Other Slices Summary CHAPTER 5: USING STRUCTS TO STRUCTURE RELATED DATA Defining and Instantiating Structs Using the Field Init Shorthand Creating Instances from Other Instances with Struct Update Syntax Using Tuple Structs Without Named Fields to Create D ifferent Types Unit-Like Structs Without Any Fields An Example Program Using Structs Refactoring with Tuples Refactoring with Structs: Adding More Meaning Adding Useful Functionality with Derived Traits Method Syntax Defining Methods Methods with More Parameters Associated Functions Multiple impl Blocks Summary CHAPTER 6: ENUMS AND PATTERN MATCHING Defining an Enum Enum Values The Option Enum and Its Advantages Over Null Values The match Control Flow Construct
10.
Patterns That Bindto Values Matching with Option<T> Matches Are Exhaustive Catch-All Patterns and the _ Placeholder Concise Control Flow with if let Summary CHAPTER 7: MANAGING GROWING PROJECTS WITH PACKAGES, CRAT ES, AND MODULES Packages and Crates Defining Modules to Control Scope and Privacy Paths for Referring to an Item in the Module Tree Exposing Paths with the pub Keyword Starting Relative Paths with super Making Structs and Enums Public Bringing Paths into Scope with the use Keyword Creating Idiomatic use Paths Providing New Names with the as Keyword Re-exporting Names with pub use Using External Packages Using Nested Paths to Clean Up Large use Lists The Glob Operator Separating Modules into Different Files Summary CHAPTER 8: COMMON COLLECTIONS Storing Lists of Values with Vectors Creating a New Vector Updating a Vector Reading Elements of Vectors
11.
Iterating Over theValues in a Vector Using an Enum to Store Multiple Types Dropping a Vector Drops Its Elements Storing UTF-8 Encoded Text with Strings What Is a String? Creating a New String Updating a String Indexing into Strings Slicing Strings Methods for Iterating Over Strings Strings Are Not So Simple Storing Keys with Associated Values in Hash Maps Creating a New Hash Map Accessing Values in a Hash Map Hash Maps and Ownership Updating a Hash Map Hashing Functions Summary CHAPTER 9: ERROR HANDLING Unrecoverable Errors with panic! Recoverable Errors with Result Matching on Different Errors Propagating Errors To panic! or Not to panic! Examples, Prototype Code, and Tests Cases in Which You Have More Information Than the Co mpiler Guidelines for Error Handling Creating Custom Types for Validation
12.
Summary CHAPTER 10: GENERICTYPES, TRAITS, AND LIFETIMES Removing Duplication by Extracting a Function Generic Data Types In Function Definitions In Struct Definitions In Enum Definitions In Method Definitions Performance of Code Using Generics Traits: Defining Shared Behavior Defining a Trait Implementing a Trait on a Type Default Implementations Traits as Parameters Returning Types That Implement Traits Using Trait Bounds to Conditionally Implement Method s Validating References with Lifetimes Preventing Dangling References with Lifetimes The Borrow Checker Generic Lifetimes in Functions Lifetime Annotation Syntax Lifetime Annotations in Function Signatures Thinking in Terms of Lifetimes Lifetime Annotations in Struct Definitions Lifetime Elision Lifetime Annotations in Method Definitions The Static Lifetime
13.
Generic Type Parameters,Trait Bounds, and Lifetimes To gether Summary CHAPTER 11: WRITING AUTOMATED TESTS How to Write Tests The Anatomy of a Test Function Checking Results with the assert! Macro Testing Equality with the assert_eq! and assert_ne! Macros Adding Custom Failure Messages Checking for Panics with should_panic Using Result<T, E> in Tests Controlling How Tests Are Run Running Tests in Parallel or Consecutively Showing Function Output Running a Subset of Tests by Name Ignoring Some Tests Unless Specifically Requested Test Organization Unit Tests Integration Tests Summary CHAPTER 12: AN I/O PROJECT: BUILDING A COMMAND LINE PROG RAM Accepting Command Line Arguments Reading the Argument Values Saving the Argument Values in Variables Reading a File Refactoring to Improve Modularity and Error Handling Separation of Concerns for Binary Projects
14.
Fixing the ErrorHandling Extracting Logic from main Splitting Code into a Library Crate Developing the Library’s Functionality with Test-Drive n Development Writing a Failing Test Writing Code to Pass the Test Working with Environment Variables Writing a Failing Test for the Case-Insensitive Sear ch Function Implementing the search_case_insensitive Function Writing Error Messages to Standard Error Instead of Sta ndard Output Checking Where Errors Are Written Printing Errors to Standard Error Summary CHAPTER 13: FUNCTIONAL LANGUAGE FEATURES: ITERATORS AND CLOSURES Closures: Anonymous Functions That Capture Their Enviro nment Capturing the Environment with Closures Closure Type Inference and Annotation Capturing References or Moving Ownership Moving Captured Values Out of Closures and the Fn Tr aits Processing a Series of Items with Iterators The Iterator Trait and the next Method Methods That Consume the Iterator Methods That Produce Other Iterators Using Closures That Capture Their Environment
15.
Improving Our I/OProject Removing a clone Using an Iterator Making Code Clearer with Iterator Adapters Choosing Between Loops and Iterators Comparing Performance: Loops vs. Iterators Summary CHAPTER 14: MORE ABOUT CARGO AND CRATES.IO Customizing Builds with Release Profiles Publishing a Crate to Crates.io Making Useful Documentation Comments Exporting a Convenient Public API with pub use Setting Up a Crates.io Account Adding Metadata to a New Crate Publishing to Crates.io Publishing a New Version of an Existing Crate Deprecating Versions from Crates.io with cargo yank Cargo Workspaces Creating a Workspace Creating the Second Package in the Workspace Installing Binaries with cargo install Extending Cargo with Custom Commands Summary CHAPTER 15: SMART POINTERS Using Box<T> to Point to Data on the Heap Using Box<T> to Store Data on the Heap Enabling Recursive Types with Boxes Treating Smart Pointers Like Regular References with De ref
16.
Following the Pointerto the Value Using Box<T> Like a Reference Defining Our Own Smart Pointer Implementing the Deref Trait Implicit Deref Coercions with Functions and Methods How Deref Coercion Interacts with Mutability Running Code on Cleanup with the Drop Trait Rc<T>, the Reference Counted Smart Pointer Using Rc<T> to Share Data Cloning an Rc<T> Increases the Reference Count RefCell<T> and the Interior Mutability Pattern Enforcing Borrowing Rules at Runtime with RefCell<T> Interior Mutability: A Mutable Borrow to an Immutabl e Value Allowing Multiple Owners of Mutable Data with Rc<T> and RefCell<T> Reference Cycles Can Leak Memory Creating a Reference Cycle Preventing Reference Cycles Using Weak<T> Summary CHAPTER 16: FEARLESS CONCURRENCY Using Threads to Run Code Simultaneously Creating a New Thread with spawn Waiting for All Threads to Finish Using join Handles Using move Closures with Threads Using Message Passing to Transfer Data Between Threads Channels and Ownership Transference Sending Multiple Values and Seeing the Receiver Wait ing
17.
Creating Multiple Producersby Cloning the Transmitt er Shared-State Concurrency Using Mutexes to Allow Access to Data from One Threa d at a Time Similarities Between RefCell<T>/Rc<T> and Mutex<T>/A rc<T> Extensible Concurrency with the Send and Sync Traits Allowing Transference of Ownership Between Threads w ith Send Allowing Access from Multiple Threads with Sync Implementing Send and Sync Manually Is Unsafe Summary CHAPTER 17: OBJECT-ORIENTED PROGRAMMING FEATURES Characteristics of Object-Oriented Languages Objects Contain Data and Behavior Encapsulation That Hides Implementation Details Inheritance as a Type System and as Code Sharing Using Trait Objects That Allow for Values of Different Types Defining a Trait for Common Behavior Implementing the Trait Trait Objects Perform Dynamic Dispatch Implementing an Object-Oriented Design Pattern Defining Post and Creating a New Instance in the Dra ft State Storing the Text of the Post Content Ensuring the Content of a Draft Post Is Empty Requesting a Review Changes the Post’s State Adding approve to Change the Behavior of content
18.
Trade-offs of theState Pattern Summary CHAPTER 18: PATTERNS AND MATCHING All the Places Patterns Can Be Used match Arms Conditional if let Expressions while let Conditional Loops for Loops let Statements Function Parameters Refutability: Whether a Pattern Might Fail to Match Pattern Syntax Matching Literals Matching Named Variables Multiple Patterns Matching Ranges of Values with ..= Destructuring to Break Apart Values Ignoring Values in a Pattern Extra Conditionals with Match Guards @ Bindings Summary CHAPTER 19: ADVANCED FEATURES Unsafe Rust Unsafe Superpowers Dereferencing a Raw Pointer Calling an Unsafe Function or Method Accessing or Modifying a Mutable Static Variable Implementing an Unsafe Trait
19.
Accessing Fields ofa Union When to Use Unsafe Code Advanced Traits Associated Types Default Generic Type Parameters and Operator Overloa ding Disambiguating Between Methods with the Same Name Using Supertraits Using the Newtype Pattern to Implement External Trai ts Advanced Types Using the Newtype Pattern for Type Safety and Abstra ction Creating Type Synonyms with Type Aliases The Never Type That Never Returns Dynamically Sized Types and the Sized Trait Advanced Functions and Closures Function Pointers Returning Closures Macros The Difference Between Macros and Functions Declarative Macros with macro_rules! for General Met aprogramming Procedural Macros for Generating Code from Attribute s How to Write a Custom derive Macro Attribute-Like Macros Function-Like Macros Summary
20.
CHAPTER 20: FINALPROJECT: BUILDING A MULTITHREADED WEB SERVER Building a Single-Threaded Web Server Listening to the TCP Connection Reading the Request A Closer Look at an HTTP Request Writing a Response Returning Real HTML Validating the Request and Selectively Responding A Touch of Refactoring Turning Our Single-Threaded Server into a Multithreaded Server Simulating a Slow Request Improving Throughput with a Thread Pool Graceful Shutdown and Cleanup Implementing the Drop Trait on ThreadPool Signaling to the Threads to Stop Listening for Jobs Summary APPENDIX A: KEYWORDS Keywords Currently in Use Keywords Reserved for Future Use Raw Identifiers APPENDIX B: OPERATORS AND SYMBOLS Operators Non-operator Symbols APPENDIX C: DERIVABLE TRAITS Debug for Programmer Output PartialEq and Eq for Equality Comparisons
21.
PartialOrd and Ordfor Ordering Comparisons Clone and Copy for Duplicating Values Hash for Mapping a Value to a Value of Fixed Size Default for Default Values APPENDIX D: USEFUL DEVELOPMENT TOOLS Automatic Formatting with rustfmt Fix Your Code with rustfix More Lints with Clippy IDE Integration Using rust-analyzer APPENDIX E: EDITIONS INDEX
22.
THE RUST PROGRAMMINGLANGUAGE 2nd Edition by Steve Klabnik and Carol Nichols, with contributions from the Rust Community
005.13/3--dc23 LC record availableat https://lccn.loc.gov/2018014097 No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, neither the authors nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.
25.
About the Authors SteveKlabnik was the lead for the Rust documentation team and was one of Rust’s core developers. A frequent speaker and a prolific open source contributor, he previously worked on projects such as Ruby and Ruby on Rails. Carol Nichols is a member of the Rust Crates.io Team and a former member 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.
26.
FOREWORD It wasn’t alwaysso clear, but the Rust programming language is fundamentally about empowerment: no matter what kind of code you are writing now, Rust empowers you to reach further, to program with confidence in a wider variety of domains than you did before. Take, for example, “systems-level” work that deals with low-level details of memory management, data representation, and concurrency. Traditionally, this realm of programming is seen as arcane, accessible to only a select few who have devoted the necessary years to learning it to avoid its infamous pitfalls. And even those who practice it do so with caution, lest their code be open to exploits, crashes, or corruption. Rust breaks down these barriers by eliminating the old pitfalls and providing a friendly, polished set of tools to help you along the way. Programmers who need to “dip down” into lower-level control can do so with Rust, without taking on the customary risk of crashes or security holes and without having to learn the fine points of a fickle toolchain. Better yet, the language is designed to guide you naturally toward reliable code that is efficient in terms of speed and memory usage. Programmers who are already working with low-level code can use Rust to raise their ambitions. For example, introducing parallelism in Rust is a relatively low-risk operation: the compiler will catch the classical mistakes for you. And you can tackle more aggressive optimizations in your code with the confidence that you won’t accidentally introduce crashes or vulnerabilities. But Rust isn’t limited to low-level systems programming. It’s expressive and ergonomic enough to make CLI apps, web servers,
27.
and many otherkinds of code quite pleasant to write—you’ll find simple examples later in the book. Working with Rust allows you to build skills that transfer from one domain to another; you can learn Rust by writing a web app, then apply those same skills to target your Raspberry Pi. This book fully embraces the potential of Rust to empower its users. It’s a friendly and approachable text intended to help you level up not just your knowledge of Rust, but also your reach and confidence as a programmer in general. So dive in, get ready to learn —and welcome to the Rust community! Nicholas Matsakis and Aaron Turon
28.
PREFACE This version ofthe text assumes you’re using Rust 1.62.0 (released 2022-06-30) or later with edition="2021" in the Cargo.toml file of all projects to configure them to use Rust 2021 edition idioms. See “Installation” on page 1 for instructions on installing or updating Rust, and see Appendix E for information on editions. The 2021 edition of the Rust language includes a number of improvements that make Rust more ergonomic and that correct some inconsistencies. On top of a general update to reflect these improvements, this rendition of the book has a number of improvements to address specific feedback: Chapter 7 contains a new quick reference section on organizing your code into multiple files with modules. Chapter 13 has new and improved closure examples that more clearly illustrate captures, the move keyword, and the Fn traits. We fixed a number of small errors and imprecise wording throughout the book. Thank you to the readers who reported them! Note that any code from earlier renditions of this book that compiled will continue to compile with the relevant edition in the project’s Cargo.toml, even as you update the Rust compiler version you’re using. That’s Rust’s backward-compatibility guarantees at work!
29.
ACKNOWLEDGMENTS We would liketo thank everyone who has worked on the Rust language for creating an amazing language worth writing a book about. We’re grateful to everyone in the Rust community for being welcoming and creating an environment worth welcoming more folks into. We’re especially thankful for everyone who read early versions of this book online and provided feedback, bug reports, and pull requests. Special thanks to Eduard-Mihai Burtescu, Alex Crichton, and JT for providing technical review, and to Karen Rustad Tölva for the cover art. Thank you to our team at No Starch, including Bill Pollock, Liz Chadwick, and Janelle Ludowise, for improving this book and bringing it to print. Carol is grateful for the opportunity to work on this book. She thanks her family for their constant love and support, especially her husband, Jake Goulding, and her daughter, Vivian.
30.
INTRODUCTION Welcome to TheRust Programming Language, an introductory book about Rust. The Rust programming language helps you write faster, more reliable software. High-level ergonomics and low-level control are often at odds in programming language design; Rust challenges that conflict. Through balancing powerful technical capacity and a great developer experience, Rust gives you the option to control low-level details (such as memory usage) without all the hassle traditionally associated with such control. Who Rust Is For Rust is ideal for many people for a variety of reasons. Let’s look at a few of the most important groups. Teams of Developers Rust is proving to be a productive tool for collaborating among large teams of developers with varying levels of systems programming knowledge. Low-level code is prone to various subtle bugs, which in most other languages can only be caught through extensive testing and careful code review by experienced developers. In Rust, the
31.
compiler plays agatekeeper role by refusing to compile code with these elusive bugs, including concurrency bugs. By working alongside the compiler, the team can spend their time focusing on the program’s logic rather than chasing down bugs. Rust also brings contemporary developer tools to the systems programming world: Cargo, the included dependency manager and build tool, makes adding, compiling, and managing dependencies painless and consistent across the Rust ecosystem. The rustfmt formatting tool ensures a consistent coding style across developers. The Rust Language Server powers integrated development environment (IDE) integration for code completion and inline error messages. By using these and other tools in the Rust ecosystem, developers can be productive while writing systems-level code. Students Rust is for students and those who are interested in learning about systems concepts. Using Rust, many people have learned about topics like operating systems development. The community is very welcoming and happy to answer students’ questions. Through efforts such as this book, the Rust teams want to make systems concepts more accessible to more people, especially those new to programming. Companies Hundreds of companies, large and small, use Rust in production for a variety of tasks, including command line tools, web services, DevOps tooling, embedded devices, audio and video analysis and transcoding, cryptocurrencies, bioinformatics, search engines, Internet of Things applications, machine learning, and even major parts of the Firefox web browser.
32.
Open Source Developers Rustis for people who want to build the Rust programming language, community, developer tools, and libraries. We’d love to have you contribute to the Rust language. People Who Value Speed and Stability Rust is for people who crave speed and stability in a language. By speed, we mean both how quickly Rust code can run and the speed at which Rust lets you write programs. The Rust compiler’s checks ensure stability through feature additions and refactoring. This is in contrast to the brittle legacy code in languages without these checks, which developers are often afraid to modify. By striving for zero-cost abstractions—higher-level features that compile to lower-level code as fast as code written manually—Rust endeavors to make safe code be fast code as well. The Rust language hopes to support many other users as well; those mentioned here are merely some of the biggest stakeholders. Overall, Rust’s greatest ambition is to eliminate the trade-offs that programmers have accepted for decades by providing safety and productivity, speed and ergonomics. Give Rust a try and see if its choices work for you.
33.
Who This BookIs For This book assumes that you’ve written code in another programming language, but doesn’t make any assumptions about which one. We’ve tried to make the material broadly accessible to those from a wide variety of programming backgrounds. We don’t spend a lot of time talking about what programming is or how to think about it. If you’re entirely new to programming, you would be better served by reading a book that specifically provides an introduction to programming. How to Use This Book In general, this book assumes that you’re reading it in sequence from front to back. Later chapters build on concepts in earlier chapters, and earlier chapters might not delve into details on a particular topic but will revisit the topic in a later chapter. You’ll find two kinds of chapters in this book: concept chapters and project chapters. In concept chapters, you’ll learn about an aspect of Rust. In project chapters, we’ll build small programs together, applying what you’ve learned so far. Chapter 2, Chapter 12, and Chapter 20 are project chapters; the rest are concept chapters. Chapter 1 explains how to install Rust, how to write a “Hello, world!” program, and how to use Cargo, Rust’s package manager and build tool. Chapter 2 is a hands-on introduction to writing a program in Rust, having you build up a number-guessing game. Here, we cover concepts at a high level, and later chapters will provide additional detail. If you want to get your hands dirty right away, Chapter 2 is the place for that. Chapter 3 covers Rust features that are similar to those of other programming languages, and in Chapter 4 you’ll learn about Rust’s ownership system. If you’re a particularly meticulous learner who prefers to learn every detail before moving on to the next, you might want to skip Chapter 2 and go straight to Chapter 3, returning to Chapter 2 when you’d like to work on a project applying the details you’ve learned.
34.
Chapter 5 discussesstructs and methods, and Chapter 6 covers enums, match expressions, and the if let control flow construct. You’ll use structs and enums to make custom types in Rust. In Chapter 7, you’ll learn about Rust’s module system and about privacy rules for organizing your code and its public application programming interface (API). Chapter 8 discusses some common collection data structures that the standard library provides, such as vectors, strings, and hash maps. Chapter 9 explores Rust’s error- handling philosophy and techniques. Chapter 10 digs into generics, traits, and lifetimes, which give you the power to define code that applies to multiple types. Chapter 11 is all about testing, which even with Rust’s safety guarantees is necessary to ensure your program’s logic is correct. In Chapter 12, we’ll build our own implementation of a subset of functionality from the grep command line tool that searches for text within files. For this, we’ll use many of the concepts we discussed in the previous chapters. Chapter 13 explores closures and iterators: features of Rust that come from functional programming languages. In Chapter 14, we’ll examine Cargo in more depth and talk about best practices for sharing your libraries with others. Chapter 15 discusses smart pointers that the standard library provides and the traits that enable their functionality. In Chapter 16, we’ll walk through different models of concurrent programming and talk about how Rust helps you program in multiple threads fearlessly. Chapter 17 looks at how Rust idioms compare to object-oriented programming principles you might be familiar with. Chapter 18 is a reference on patterns and pattern matching, which are powerful ways of expressing ideas throughout Rust programs. Chapter 19 contains a smorgasbord of advanced topics of interest, including unsafe Rust, macros, and more about lifetimes, traits, types, functions, and closures. In Chapter 20, we’ll complete a project in which we’ll implement a low-level multithreaded web server!
35.
Finally, some appendixescontain useful information about the language in a more reference-like format. Appendix A covers Rust’s keywords, Appendix B covers Rust’s operators and symbols, Appendix C covers derivable traits provided by the standard library, Appendix D covers some useful development tools, and Appendix E explains Rust editions. There is no wrong way to read this book: if you want to skip ahead, go for it! You might have to jump back to earlier chapters if you experience any confusion. But do whatever works for you. An important part of the process of learning Rust is learning how to read the error messages the compiler displays: these will guide you toward working code. As such, we’ll provide many examples that don’t compile along with the error message the compiler will show you in each situation. Know that if you enter and run a random example, it may not compile! Make sure you read the surrounding text to see whether the example you’re trying to run is meant to error. In most situations, we’ll lead you to the correct version of any code that doesn’t compile. Resources and How to Contribute to This Book This book is open source. If you find an error, please don’t hesitate to file an issue or send a pull request on GitHub at https://github.com/ rust-lang/book. Please see CONTRIBUTING.md at https://github.c om/rust-lang/book/blob/main/CONTRIBUTING.md for more details. The source code for the examples in this book, errata, and other information are available at https://nostarch.com/rust-programmi ng-language-2nd-edition.
36.
1 GETTING STARTED Let’s startyour Rust journey! There’s a lot to learn, but every journey starts somewhere. In this chapter, we’ll discuss: Installing Rust on Linux, macOS, and Windows Writing a program that prints Hello, world! Using cargo, Rust’s package manager and build system Installation The first step is to install Rust. We’ll download Rust through rustup, a command line tool for managing Rust versions and associated tools. You’ll need an internet connection for the download. NOTE If you prefer not to use rustup for some reason, please see the Other Rust Installation Methods page at https://forge.rust-l ang.org/infra/other-installation-methods.html for more options. The following steps install the latest stable version of the Rust compiler. Rust’s stability guarantees ensure that all the examples in the book that compile will continue to compile with newer Rust versions. The output might differ slightly between versions because
37.
Rust often improveserror messages and warnings. In other words, any newer, stable version of Rust you install using these steps should work as expected with the content of this book. COMMAND LINE NOTATION In this chapter and throughout the book, we’ll show some commands used in the terminal. Lines that you should enter in a terminal all start with $. You don’t need to type the $ character; it’s the command line prompt shown to indicate the start of each command. Lines that don’t start with $ typically show the output of the previous command. Additionally, PowerShell-specific examples will use > rather than $. Installing rustup on Linux or macOS If you’re using Linux or macOS, open a terminal and enter the following command: $ curl --proto '=https' --tlsv1.3 https://sh.rustup.rs -sSf | sh The command downloads a script and starts the installation of the rustup tool, which installs the latest stable version of Rust. You might be prompted for your password. If the install is successful, the following line will appear: Rust is installed now. Great! You will also need a linker, which is a program that Rust uses to join its compiled outputs into one file. It is likely you already have one. If you get linker errors, you should install a C compiler, which will typically include a linker. A C compiler is also useful because some common Rust packages depend on C code and will need a C compiler. On macOS, you can get a C compiler by running: $ xcode-select --install
38.
Linux users shouldgenerally install GCC or Clang, according to their distribution’s documentation. For example, if you use Ubuntu, you can install the build-essential package. Installing rustup on Windows On Windows, go to https://www.rust-lang.org/tools/install and follow the instructions for installing Rust. At some point in the installation, you’ll receive a message explaining that you’ll also need the MSVC build tools for Visual Studio 2013 or later. To acquire the build tools, you’ll need to install Visual Studio 2022 from https://visualstudio.microsoft.com/downloads. When asked which workloads to install, include: “Desktop Development with C++” The Windows 10 or 11 SDK The English language pack component, along with any other language pack of your choosing The rest of this book uses commands that work in both cmd.exe and PowerShell. If there are specific differences, we’ll explain which to use. Troubleshooting To check whether you have Rust installed correctly, open a shell and enter this line: $ rustc --version You should see the version number, commit hash, and commit date for the latest stable version that has been released, in the following format: rustc x.y.z (abcabcabc yyyy-mm-dd)
39.
If you seethis information, you have installed Rust successfully! If you don’t see this information, check that Rust is in your %PATH% system variable as follows. In Windows CMD, use: > echo %PATH% In PowerShell, use: > echo $env:Path In Linux and macOS, use: $ echo $PATH If that’s all correct and Rust still isn’t working, there are a number of places you can get help. Find out how to get in touch with other Rustaceans (a silly nickname we call ourselves) on the community page at https://www.rust-lang.org/community. Updating and Uninstalling Once Rust is installed via rustup, updating to a newly released version is easy. From your shell, run the following update script: $ rustup update To uninstall Rust and rustup, run the following uninstall script from your shell: $ rustup self uninstall Local Documentation The installation of Rust also includes a local copy of the documentation so that you can read it offline. Run rustup doc to open the local documentation in your browser.
40.
Any time atype or function is provided by the standard library and you’re not sure what it does or how to use it, use the application programming interface (API) documentation to find out! Hello, World! Now that you’ve installed Rust, it’s time to write your first Rust program. It’s traditional when learning a new language to write a little program that prints the text Hello, world! to the screen, so we’ll do the same here! NOTE This book assumes basic familiarity with the command line. Rust makes no specific demands about your editing or tooling or where your code lives, so if you prefer to use an integrated development environment (IDE) instead of the command line, feel free to use your favorite IDE. Many IDEs now have some degree of Rust support; check the IDE’s documentation for details. The Rust team has been focusing on enabling great IDE support via rust-analyzer. See Appendix D for more details. Creating a Project Directory You’ll start by making a directory to store your Rust code. It doesn’t matter to Rust where your code lives, but for the exercises and projects in this book, we suggest making a projects directory in your home directory and keeping all your projects there. Open a terminal and enter the following commands to make a projects directory and a directory for the “Hello, world!” project within the projects directory. For Linux, macOS, and PowerShell on Windows, enter this: $ mkdir ~/projects $ cd ~/projects $ mkdir hello_world $ cd hello_world
41.
For Windows CMD,enter this: > mkdir "%USERPROFILE%projects" > cd /d "%USERPROFILE%projects" > mkdir hello_world > cd hello_world Writing and Running a Rust Program Next, make a new source file and call it main.rs. Rust files always end with the .rs extension. If you’re using more than one word in your filename, the convention is to use an underscore to separate them. For example, use hello_world.rs rather than helloworld.rs. Now open the main.rs file you just created and enter the code in Li sting 1-1. main.rs fn main() { println!("Hello, world!"); } Listing 1-1: A program that prints Hello, world! Save the file and go back to your terminal window in the ~/projects/hello_world directory. On Linux or macOS, enter the following commands to compile and run the file: $ rustc main.rs $ ./main Hello, world! On Windows, enter the command .main.exe instead of ./main: > rustc main.rs > .main.exe Hello, world! Regardless of your operating system, the string Hello, world! should print to the terminal. If you don’t see this output, refer back to
42.
“Troubleshooting” on page3 for ways to get help. If Hello, world! did print, congratulations! You’ve officially written a Rust program. That makes you a Rust programmer—welcome! Anatomy of a Rust Program Let’s review this “Hello, world!” program in detail. Here’s the first piece of the puzzle: fn main() { } These lines define a function named main. The main function is special: it is always the first code that runs in every executable Rust program. Here, the first line declares a function named main that has no parameters and returns nothing. If there were parameters, they would go inside the parentheses (). The function body is wrapped in {}. Rust requires curly brackets around all function bodies. It’s good style to place the opening curly bracket on the same line as the function declaration, adding one space in between. NOTE If you want to stick to a standard style across Rust projects, you can use an automatic formatter tool called rustfmt to format your code in a particular style (more on rustfmt in Appendix D). The Rust team has included this tool with the standard Rust distribution, as rustc is, so it should already be installed on your computer! The body of the main function holds the following code: println!("Hello, world!"); This line does all the work in this little program: it prints text to the screen. There are four important details to notice here. First, Rust style is to indent with four spaces, not a tab.
43.
Second, println! callsa Rust macro. If it had called a function instead, it would be entered as println (without the !). We’ll discuss Rust macros in more detail in Chapter 19. For now, you just need to know that using a ! means that you’re calling a macro instead of a normal function and that macros don’t always follow the same rules as functions. Third, you see the "Hello, world!" string. We pass this string as an argument to println!, and the string is printed to the screen. Fourth, we end the line with a semicolon (;), which indicates that this expression is over and the next one is ready to begin. Most lines of Rust code end with a semicolon. Compiling and Running Are Separate Steps You’ve just run a newly created program, so let’s examine each step in the process. Before running a Rust program, you must compile it using the Rust compiler by entering the rustc command and passing it the name of your source file, like this: $ rustc main.rs If you have a C or C++ background, you’ll notice that this is similar to gcc or clang. After compiling successfully, Rust outputs a binary executable. On Linux, macOS, and PowerShell on Windows, you can see the executable by entering the ls command in your shell: $ ls main main.rs On Linux and macOS, you’ll see two files. With PowerShell on Windows, you’ll see the same three files that you would see using CMD. With CMD on Windows, you would enter the following: > dir /B %= the /B option says to only show the file names =% main.exe
44.
main.pdb main.rs This shows thesource code file with the .rs extension, the executable file (main.exe on Windows, but main on all other platforms), and, when using Windows, a file containing debugging information with the .pdb extension. From here, you run the main or main.exe file, like this: $ ./main # or .main.exe on Windows If your main.rs is your “Hello, world!” program, this line prints Hello, world! to your terminal. If you’re more familiar with a dynamic language, such as Ruby, Python, or JavaScript, you might not be used to compiling and running a program as separate steps. Rust is an ahead-of-time compiled language, meaning you can compile a program and give the executable to someone else, and they can run it even without having Rust installed. If you give someone a .rb, .py, or .js file, they need to have a Ruby, Python, or JavaScript implementation installed (respectively). But in those languages, you only need one command to compile and run your program. Everything is a trade-off in language design. Just compiling with rustc is fine for simple programs, but as your project grows, you’ll want to manage all the options and make it easy to share your code. Next, we’ll introduce you to the Cargo tool, which will help you write real-world Rust programs. Hello, Cargo! Cargo is Rust’s build system and package manager. Most Rustaceans use this tool to manage their Rust projects because Cargo handles a lot of tasks for you, such as building your code, downloading the libraries your code depends on, and building those libraries. (We call the libraries that your code needs dependencies.) The simplest Rust programs, like the one we’ve written so far, don’t have any dependencies. If we had built the “Hello, world!”
PLEASE READ THISBEFORE 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.
47.
1.C. The ProjectGutenberg 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
48.
Gutenberg” appears, orwith 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
49.
containing a partof 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
50.
payments must bepaid 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
51.
law in creatingthe 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 damaged disk or other medium, a computer virus, or computer codes that damage or cannot be read by your equipment. 1.F.2. LIMITED WARRANTY, DISCLAIMER OF DAMAGES - Except for the “Right of Replacement or Refund” described in paragraph 1.F.3, the Project Gutenberg Literary Archive Foundation, the owner of the Project Gutenberg™ trademark, and any other party distributing a Project Gutenberg™ electronic work under this agreement, disclaim all liability to you for damages, costs and expenses, including legal fees. YOU AGREE THAT YOU HAVE NO REMEDIES FOR NEGLIGENCE, STRICT LIABILITY, BREACH OF WARRANTY OR BREACH OF CONTRACT EXCEPT THOSE PROVIDED IN PARAGRAPH 1.F.3. YOU AGREE THAT THE FOUNDATION, THE TRADEMARK OWNER, AND ANY DISTRIBUTOR UNDER THIS AGREEMENT WILL NOT BE LIABLE TO YOU FOR ACTUAL, DIRECT, INDIRECT, CONSEQUENTIAL, PUNITIVE OR INCIDENTAL DAMAGES EVEN IF YOU GIVE NOTICE OF THE POSSIBILITY OF SUCH DAMAGE. 1.F.3. LIMITED RIGHT OF REPLACEMENT OR REFUND - If you discover a defect in this electronic work within 90 days of receiving it, you can receive a refund of the money (if any) you paid for it by sending a written explanation to the person you received the work from. If you received the work on a physical medium, you must return the medium with your written explanation. The person or entity that provided you with the defective work may elect to provide a replacement copy in lieu of a refund. If you received the work electronically, the person or entity providing it to you may choose to give you a second opportunity to receive the work electronically in lieu of a refund.
52.
If the secondcopy is also defective, you may demand a refund in writing without further opportunities to fix the problem. 1.F.4. Except for the limited right of replacement or refund set forth in paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PURPOSE. 1.F.5. Some states do not allow disclaimers of certain implied warranties or the exclusion or limitation of certain types of damages. If any disclaimer or limitation set forth in this agreement violates the law of the state applicable to this agreement, the agreement shall be interpreted to make the maximum disclaimer or limitation permitted by the applicable state law. The invalidity or unenforceability of any provision of this agreement shall not void the remaining provisions. 1.F.6. INDEMNITY - You agree to indemnify and hold the Foundation, the trademark owner, any agent or employee of the Foundation, anyone providing copies of Project Gutenberg™ electronic works in accordance with this agreement, and any volunteers associated with the production, promotion and distribution of Project Gutenberg™ electronic works, harmless from all liability, costs and expenses, including legal fees, that arise directly or indirectly from any of the following which you do or cause to occur: (a) distribution of this or any Project Gutenberg™ work, (b) alteration, modification, or additions or deletions to any Project Gutenberg™ work, and (c) any Defect you cause. Section 2. Information about the Mission of Project Gutenberg™
53.
Project Gutenberg™ issynonymous with the free distribution of electronic works in formats readable by the widest variety of computers including obsolete, old, middle-aged and new computers. It exists because of the efforts of hundreds of volunteers and donations from people in all walks of life. Volunteers and financial support to provide volunteers with the assistance they need are critical to reaching Project Gutenberg™’s goals and ensuring that the Project Gutenberg™ collection will remain freely available for generations to come. In 2001, the Project Gutenberg Literary Archive Foundation was created to provide a secure and permanent future for Project Gutenberg™ and future generations. To learn more about the Project Gutenberg Literary Archive Foundation and how your efforts and donations can help, see Sections 3 and 4 and the Foundation information page at www.gutenberg.org. Section 3. Information about the Project Gutenberg Literary Archive Foundation The Project Gutenberg Literary Archive Foundation is a non- profit 501(c)(3) educational corporation organized under the laws of the state of Mississippi and granted tax exempt status by the Internal Revenue Service. The Foundation’s EIN or federal tax identification number is 64-6221541. Contributions to the Project Gutenberg Literary Archive Foundation are tax deductible to the full extent permitted by U.S. federal laws and your state’s laws. The Foundation’s business office is located at 809 North 1500 West, Salt Lake City, UT 84116, (801) 596-1887. Email contact links and up to date contact information can be found at the Foundation’s website and official page at www.gutenberg.org/contact
54.
Section 4. Informationabout Donations to the Project Gutenberg Literary Archive Foundation Project Gutenberg™ depends upon and cannot survive without widespread public support and donations to carry out its mission of increasing the number of public domain and licensed works that can be freely distributed in machine-readable form accessible by the widest array of equipment including outdated equipment. Many small donations ($1 to $5,000) are particularly important to maintaining tax exempt status with the IRS. The Foundation is committed to complying with the laws regulating charities and charitable donations in all 50 states of the United States. Compliance requirements are not uniform and it takes a considerable effort, much paperwork and many fees to meet and keep up with these requirements. We do not solicit donations in locations where we have not received written confirmation of compliance. To SEND DONATIONS or determine the status of compliance for any particular state visit www.gutenberg.org/donate. While we cannot and do not solicit contributions from states where we have not met the solicitation requirements, we know of no prohibition against accepting unsolicited donations from donors in such states who approach us with offers to donate. International donations are gratefully accepted, but we cannot make any statements concerning tax treatment of donations received from outside the United States. U.S. laws alone swamp our small staff. Please check the Project Gutenberg web pages for current donation methods and addresses. Donations are accepted in a number of other ways including checks, online payments and
55.
credit card donations.To donate, please visit: www.gutenberg.org/donate. Section 5. General Information About Project Gutenberg™ electronic works Professor Michael S. Hart was the originator of the Project Gutenberg™ concept of a library of electronic works that could be freely shared with anyone. For forty years, he produced and distributed Project Gutenberg™ eBooks with only a loose network of volunteer support. Project Gutenberg™ eBooks are often created from several printed editions, all of which are confirmed as not protected by copyright in the U.S. unless a copyright notice is included. Thus, we do not necessarily keep eBooks in compliance with any particular paper edition. Most people start at our website which has the main PG search facility: www.gutenberg.org. This website includes information about Project Gutenberg™, including how to make donations to the Project Gutenberg Literary Archive Foundation, how to help produce our new eBooks, and how to subscribe to our email newsletter to hear about new eBooks.