3 Code flow

 

This chapter covers

  • Discussing pattern matching
  • Handling errors with pattern matching
  • Reviewing Rust’s functional programming patterns

The subsequent core building blocks we need to discuss are pattern matching and functional programming. Pattern matching allows us to control the code flow, unwrap or destructure values, and handle optional cases. Functional programming lets us build software around the unit of a function, which is one of the most basic and easiest-to-understand abstractions.

Each building block is distinct, but each can be combined in many ways to create entirely new abstractions. We’ll tie these building blocks together to create more elaborate design patterns by combining them in various ways. To use an analogy, in cooking, we employ four essential elements in different combinations from multiple sources to create delicious foods: salt, fat, acid, and heat. Before making patterns based on these elements, we must understand them in depth.

3.1 Pattern matching

Up to now we’ve discussed generics and traits which make up Rust’s core compile-time features. Pattern matching is a runtime feature that enables a variety of nice code flow patterns. We can match types, values, enum variants, and more. Rust’s pattern matching is powerful because it supports several different kinds of matching, but most importantly it enables clean, functional programming patterns.

3.1.1 Basics of pattern matching

3.1.2 Clean matches with the ? operator

3.2 Functional programming patterns

3.2.1 Basics of FP in Rust

3.2.2 Closure variable capture

3.2.3 Examining iterators

3.2.4 Iterator features

3.3 Summary

sitemap