Book Image

Hands-On Functional Programming in Rust

By : Andrew Johnson
Book Image

Hands-On Functional Programming in Rust

By: Andrew Johnson

Overview of this book

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.
Table of Contents (12 chapters)

What this book covers

Chapter 1, Functional Programming – a Comparison, introduces functional programming in Rust. Comparisons are drawn between functional style and other paradigms that are prevalent or influential to Rust. The chapter also serves as a brief outline of topics that will appear later in the book.

Chapter 2, Functional Control Flow, introduces Rust control flow structures while explaining how they are relevant to the functional style of programming. The expression-centric nature of functional programming and Rust is illustrated through examples. Limiting as it may be, the chapter also begins an ongoing project using only the procedural expression style of programming.

Chapter 3, Functional Data Structures, introduces the reader to the various, highly expressive data types available in Rust. Notably, the enum type is introduced, which holds particular significance in functional programming. The project continues to grow to incorporate a variety of these data types.

Chapter 4, Generics and Polymorphism, explains the concepts of parameterization of data (generics) and parameterization of control flow (polymorphism). Parameterization and its natural interaction with traits reduces the programmer's burden, but the syntax can become overwhelming. Some approaches to reduce or mitigate parameter explosion are introduced. The ongoing project again grows to incorporate these features.

Chapter 5, Code Organization and Application Architecture, talks about some architectural concerns, recommendations, and best practices. Designing and managing the implementation of a software project is not formulaic. No project is the same, and few are highly similar, thus no engineering procedure can capture the nuances of software development. In this chapter, we provide the best tools available, and specifically, the best that functional programming has to offer.

Chapter 6, Mutability, Ownership, and Pure Functions, digs into some of the more unique features in Rust. This chapter introduces the concepts of ownership and lifetimes, which are common stumbling blocks when learning Rust. The functional concepts of immutability and pure functions are also introduced to help untangle some of the spaghetti that a naive Rust programmer might generate when attempting to circumvent the rules of ownership in Rust.

Chapter 7, Design Patterns, lists as many functional programming cheat codes that can fit into a single chapter. The concept of functors and monads are explained with examples and some casual definitions. The chapter also briefly introduces the style of functional reactive programming and uses it to build a quick and dirty web framework.

Chapter 8, Implementing Concurrency, explains how to do multiple things at the same time. Most of the chapter is spent clarifying the differences and relative strengths and weaknesses between subprocesses, forked processes, and threads. The Rust thread concurrency model is then assumed and more information is provided to clarify Rust-specific logic regarding threads. Toward the end of the chapter, the actor model of concurrency is introduced, which is a robust model of concurrency that can adapt to most situations and programming paradigms.

Chapter 9, Performance, Debugging, and Metaprogramming, wraps up the book with some miscellaneous tips for programming in Rust. The performance tips are not particularly functional, but rather concerned primarily with language-specific details, general advice, or relevant bits of computer science. Debugging introduces many tips on how to prevent bugs. Also, how to use an interactive debugger is explained through examples. Metaprogramming explains precisely how Rust macros and procedural macros work. This is a great feature of Rust, but is not documented well, so it might be scary to approach.