Object-oriented programming (OOP) is a way of modeling programs. Objects came from Simula in the 1960s. Those objects influenced Alan Kay’s programming architecture in which objects pass messages to each other. He coined the term object-oriented programming in 1967 to describe this architecture. Many competing definitions describe what OOP is; some definitions would classify Rust as object oriented, but other definitions would not. In this chapter, we’ll explore certain characteristics that are commonly considered object oriented and how those characteristics translate to idiomatic Rust. We’ll then show you how to implement an object-oriented design pattern in Rust and discuss the trade-offs of doing so versus implementing a solution using some of Rust’s strengths instead.
Language Reference
The Rust Programming Language
Foreword
Introduction
Getting Started
Programming a Guessing Game
Common Programming Concepts
Understanding Ownership
Using Structs to Structure Related Data
Enums and Pattern Matching
Managing Growing Projects with Packages, Crates, and Modules
Packages and Crates
Defining Modules to Control Scope and Privacy
Paths for Referring to an Item in the Module Tree
Bringing Paths Into Scope with the use Keyword
Separating Modules into Different Files
Common Collections
Storing Lists of Values with Vectors
Storing UTF-8 Encoded Text with Strings
Storing Keys with Associated Values in Hash Maps
Error Handling
Generic Types, Traits, and Lifetimes
Writing Automated Tests
An I/O Project: Building a Command Line Program
Accepting Command Line Arguments
Reading a File
Refactoring to Improve Modularity and Error Handling
Developing the Library’s Functionality with Test Driven Development
Working with Environment Variables
Writing Error Messages to Standard Error Instead of Standard Output
Functional Language Features: Iterators and Closures
Closures: Anonymous Functions that Can Capture Their Environment
Processing a Series of Items with Iterators
Improving Our I/O Project
Comparing Performance: Loops vs. Iterators
More about Cargo and Crates.io
Customizing Builds with Release Profiles
Publishing a Crate to Crates.io
Cargo Workspaces
Installing Binaries from Crates.io with cargo install
Extending Cargo with Custom Commands
Smart Pointers
Using Box to Point to Data on the Heap
Treating Smart Pointers Like Regular References with the Deref Trait
Running Code on Cleanup with the Drop Trait
Rc, the Reference Counted Smart Pointer
RefCell and the Interior Mutability Pattern
Reference Cycles Can Leak Memory
Fearless Concurrency
Using Threads to Run Code Simultaneously
Using Message Passing to Transfer Data Between Threads
Shared-State Concurrency
Extensible Concurrency with the Sync and Send Traits
Object Oriented Programming Features of Rust
Characteristics of Object-Oriented Languages
Using Trait Objects That Allow for Values of Different Types
Implementing an Object-Oriented Design Pattern
Patterns and Matching
All the Places Patterns Can Be Used
Refutability: Whether a Pattern Might Fail to Match
Pattern Syntax
Advanced Features
Final Project: Building a Multithreaded Web Server
Building a Single-Threaded Web Server
Turning Our Single-Threaded Server into a Multithreaded Server
Graceful Shutdown and Cleanup
Appendix
Object Oriented Programming Features of Rust
2020-01-31
◂
Fearless Concurrency
Patterns and Matching
▸