liveBook · Manning

Return-value error handling

As explained in Rust Servers, Services, and Apps

Return-value error handling is an error-handling approach in which operations report success or failure through their returned values.

Return-value error handling reports success or failure through returned values instead of exceptions. Rust uses Result<T, E>, making both outcomes explicit. section 5.2

A Result-based implementation supports:

  • Matching Ok(T) and Err(E) to handle successful and failed computations. section 5.2
  • Using the ? operator to propagate errors while reducing match boilerplate. section 5.2

Parsing and serving errors

A square function returns Ok(4) for "2" and Err(ParseIntError) for "INVALID". An Actix Web handler similarly returns Result<HttpResponse, Error>; opening a missing file propagates an operating-system error instead of returning a success response.

How Rust Servers, Services, and Apps covers it

How deep it goes
Explains Result, match, ?, custom errors, and HTTP-handler propagation through executable parsing, file, and server examples.
Examples
Demonstrates integer parsing, squaring, file writing, custom errors, and an Actix Web endpoint reading a file.
What you can do after
Apply it via Rust Result and the ? operator to handle parsing, I/O, custom errors, and Actix Web responses.
How it connects
Connects Result enums, pattern matching, error propagation, custom errors, file I/O, and HTTP responses.

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.