liveBook · Manning

Match statement

As explained in Rust Servers, Services, and Apps

A match statement is a Rust control-flow construct that selects behavior by matching a value against patterns.

A match statement branches on Result variants, extracting successful values with Ok and handling failures with Err. section 5.2

Square a parsed integer

The square function matches val.parse::<i32>(), squares the number in the Ok arm, and returns the parsing error unchanged in the Err arm. Calling it with "2" produces Ok(4); calling it with "INVALID" produces a ParseIntError.

How Rust Servers, Services, and Apps covers it

How deep it goes
Explains worked Result branches, custom error formatting, and HTTP propagation before introducing the shorter ? operator.
Examples
Uses integer parsing, squaring, custom parse and I/O errors, and an Actix Web file-reading handler.
What you can do after
Apply match statements to branch on Rust Result values and propagate failures through Actix Web handlers.
How it connects
Connects match statements to Result, enums, error handling, the ? operator, and Actix Web.

Also explained in

Compare how 2 books explain match statement →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust Servers, Services, and Apps. The section links point to the parts of the book it draws on.