Hands-On Functional Programming in Rust

Book description

Explore the support Rust offers for creating functional applications in Rust. Learn about various design patterns, implementing concurrency, metaprogramming, and so on in the process

About This Book
  • Learn generics, organization, and design patterns in functional programming
  • Modularize your applications and make them highly reusable and testable using functional design patterns
  • Get familiar with complex concepts such as metaprogramming, concurrency, and immutability
Who This Book Is For

This book is for Rust developers who are comfortable with the language and now want to improve their coding abilities by learning advanced functional techniques to enhance their skillset and create robust and testable apps.

What You Will Learn
  • How Rust supports the use of basic functional programming principles
  • Use functional programming to handle concurrency with elegance
  • Read and interpret complex type signatures for types and functions
  • Implement powerful abstractions using meta programming in Rust
  • Create quality code formulaically using Rust's functional design patterns
  • Master Rust's complex ownership mechanisms particularly for mutability
In Detail

Functional programming allows developers to divide programs into smaller, reusable components that ease the creation, testing, and maintenance of software as a whole. Combined with the power of Rust, you can develop robust and scalable applications that fulfill modern day software requirements. This book will help you discover all the Rust features that can be used to build software in a functional way.

We begin with a brief comparison of the functional and object-oriented approach to different problems and patterns. We then quickly look at the patterns of control flow, data the abstractions of these unique to functional programming. The next part covers how to create functional apps in Rust; mutability and ownership, which are exclusive to Rust, are also discussed. Pure functions are examined next and you'll master closures, their various types, and currying. We also look at implementing concurrency through functional design principles and metaprogramming using macros. Finally, we look at best practices for debugging and optimization.

By the end of the book, you will be familiar with the functional approach of programming and will be able to use these techniques on a daily basis.

Style and approach

Step-by-step guide covering advanced concepts and building functional applications in Rust

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Hands-On Functional Programming in Rust
  3. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  4. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Conventions used
    4. Get in touch
      1. Reviews
  6. Functional Programming – a Comparison
    1. Technical requirements
    2. Reducing code weight and complexity
      1. Making generics more generic
      2. Functions as values
      3. Iterators
      4. Compact legible expressions
    3. Strict abstraction means safe abstraction
      1. Scoped data binding
      2. Algebraic datatypes
      3. Mixing object-oriented programming and functional programming
    4. Improving project architecture
      1. File hierarchy, modules, and namespace design
      2. Functional design patterns
      3. Metaprogramming
    5. Summary
    6. Questions
    7. Further reading
  7. Functional Control Flow
    1. Technical requirements
    2. Designing the program
      1. Gathering project requirements
      2. Architecting a code map from requirements
        1. Creating a Rust project
        2. Writing stubs for each program requirement
    3. Implementing program logic
      1. Filling in the blanks
        1. Parsing input and storing as building description and floor requests
        2. Updating location, velocity, and acceleration
        3. If the next floor request in the queue is satisfied, then remove it from the queue
        4. Adjusting motor control to process the next floor request
        5. Printing real-time statistics
        6. Printing summary
      2. Breaking down long segments into components
      3. Searching for abstractions
    4. Writing tests
      1. Unit testing
      2. Integration testing
    5. Summary
    6. Questions
  8. Functional Data Structures
    1. Technical requirements
    2. Adjusting to changing the scope of the project
      1. Gathering new project requirements
      2. Architecting a change map from requirements
        1. Translating expectations into requirements
        2. Translating requirements into a change map
    3. Mapping requirements directly to code
      1. Writing the physics simulator
      2. Writing the motor controller
      3. Writing the executable to run a simulation
      4. Writing the executable to analyze a simulation
    4. Running simulations and analyzing data
    5. Summary
    6. Questions
  9. Generics and Polymorphism
    1. Technical requirements
    2. Staying productive during downtime
    3. Learning about generics
      1. Investigating generics
      2. Investigating parametric polymorphism
      3. Investigating generalized algebraic datatypes
      4. Investigating parametric lifetimes
        1. Defining lifetimes on ground types
        2. Defining lifetimes on generic types
        3. Defining lifetimes on traits
        4. Defining lifetime subtyping
      5. Investigating parametric types
    4. Applying parameterization concepts
      1. Parameterizing data
      2. Parameterizing functions and trait objects
      3. Parametric traits and implementations
    5. Summary
    6. Questions
  10. Code Organization and Application Architecture
    1. Technical requirements
    2. Shipping a product without sacrificing quality
    3. Reorganizing the project
      1. Planning content of files by type
        1. Organizing the motor_controllers.rs module
        2. Organizing the buildings.rs module
      2. Planning content of files by purpose
        1. Organizing the motion_controllers.rs module
        2. Organizing the trip_planning.rs module
        3. Organizing the elevator_drivers.rs module
      3. Planning content of files by layer
        1. Organizing the physics.rs module
        2. Organizing the data_recorder.rs module
      4. Planning the content of files by convenience
        1. Organizing the simulate_trip.rs executable
        2. Organizing the analyze_trip.rs executable
        3. Organizing the operate_elevator.rs executable
    4. Mapping code changes and additions
      1. Developing code by type
        1. Writing the motor_controllers.rs module
        2. Writing the buildings.rs module
      2. Developing code by purpose
        1. Writing the motion_controllers.rs module
        2. Writing the trip_planning.rs module
        3. Writing the elevator_drivers.rs module
      3. Developing code by layer
        1. Writing the physics.rs module
        2. Writing the data_recorders.rs module
      4. Developing code by convenience
        1. Writing the simulate_trip.rs executable
        2. Writing the analyze_trip.rs executable
        3. Writing the operate_elevator.rs executable
      5. Reflecting on the project structure
    5. Summary
    6. Questions
  11. Mutability, Ownership, and Pure Functions
    1. Technical requirements
    2. Recognizing anti-patterns of ownership
      1. Inspecting the microcontroller drivers
      2. Inspecting the type and trait definitions
        1. Defining the OverrideCode enum
        2. Defining the ErrorCode enum
        3. Defining the AuthorizedSession struct and deconstructor
        4. Authorizing sessions
        5. Checking errors and resetting state
        6. Privileged commands
        7. Normal commands
        8. Querying library and session state
      3. Inspecting the foreign library tests
        1. Issuing override codes
        2. Accessing status information and sessions
        3. Deactivating active sessions
        4. Issuing normal commands
        5. Issuing privileged commands
        6. Denying unauthorized commands
      4. Inspecting the Rust tests
        1. Rust authorization with sessions
        2. Rust sharing session reference
        3. Privileged commands
        4. Unprivileged commands
        5. Denying access to privileged commands
    3. Learning the rules of ownership
      1. When the owner goes out of scope, the value will be dropped
    4. Using immutable data
      1. Fixing the hard-to-reproduce bug
      2. Preventing hard-to-reproduce bugs
    5. Using pure functions
    6. Summary
    7. Questions
  12. Design Patterns
    1. Technical requirements
    2. Using the functor pattern
    3. Using the monad pattern
    4. Using the combinator pattern
      1. Parser combinators
    5. Using the lazy evaluation pattern
    6. Summary
    7. Questions
  13. Implementing Concurrency
    1. Technical requirements
    2. Using subprocess concurrency
    3. Understanding nix fork concurrency
    4. Using thread concurrency
    5. Understanding Send and Sync traits
    6. Using functional design for concurrency
    7. Summary
    8. Questions
  14. Performance, Debugging, and Metaprogramming
    1. Technical requirements
    2. Writing faster code
      1. Compiling with release mode
      2. Doing less work
      3. Optimizing the code that needs it – profiling
        1. For a code rarely executed, performance is not affected
        2. Multiples of small numbers are also small numbers
        3. Measuring first, to optimize it
      4. Putting the fridge next to the computer
      5. Capping the Big O
        1. Constanting no growth
        2. Logarithmic growth
        3. Polynomial growth
        4. Exponential growth
      6. Referencing data is faster
    3. Preventing bugs with defensive coding
      1. Using Option and Result instead of panic!
      2. Using typesafe interfaces instead of stringly typed interfaces
      3. Using the heartbeat pattern for long running processes
      4. Validating input and output
    4. Finding and fixing bugs
    5. Metaprogramming
    6. Summary
    7. Questions
  15. Assessments
    1. Functional Programming – a Comparison
    2. Functional Control Flow
    3. Functional Data Structures
    4. Generics and Polymorphism
    5. Code Organization and Application Architecture
    6. Mutability, Ownership, and Pure Functions
    7. Design Patterns
    8. Implementing Concurrency
    9. Performance, Debugging, and Metaprogramming
  16. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Hands-On Functional Programming in Rust
  • Author(s): Andrew Johnson
  • Release date: May 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781788839358