liveBook · Manning

Result type

As explained in Rust Servers, Services, and Apps

A Result is a Rust standard-library generic type representing either successful output or an error.

Rust’s generic `Result<T, E>` carries either a success value of type `T` or an error. Functions and methods that can fail commonly return it so callers can inspect the outcome. `unwrap()` extracts a success value but panics on error; `?` can propagate errors from functions that return `Result`. section 2.2.2 section 7.2

Server examples use `Result` around operations that can fail:

  • TCP server and client code calls `unwrap()` on bind, connect, read, and write operations. This simplifies the examples, but is not presented as appropriate real-world handling of recoverable failures. section 2.2.2
  • An Actix server’s `main` function returns a `Result`; the example uses `?` to propagate a bind error. section 7.2
  • Actix Web error handling can convert custom errors into HTTP responses, using `ResponseError` and `From` implementations. section 5.7
use actix_files as fs;                                   #1
use actix_web::{error, web, App, Error, HttpResponse, HttpServer, Result};
 
#[actix_web::main]
async fn main() -> std::io::Result<()> {                 #2
    let addr = env::var("SERVER_ADDR").unwrap_or_else(|_|
    "127.0.0.1:8080".to_string());
    println!("Listening on: {}, open browser and visit have a try!",addr);
    HttpServer::new(|| {
        App::new().service(fs::Files::new(
        "/static", "./static").show_files_listing())   #3
    })
    .bind(addr)?                                         #4
    .run()                                               #5
    .await                                               #6
}

The server entry point returns `std::io::Result<()>` and propagates a bind failure with `?`. section 7.2

How Rust Servers, Services, and Apps covers it

How deep it goes
Defines success and error values, demonstrates `unwrap` and `?`, and summarizes combinators and conversions without detailing their implementation.
Examples
Uses `unwrap` on TCP bind, connect, read, and write operations, and shows `?` in an Actix server’s `main` function.
What you can do after
Apply it via Rust function signatures to return success values or errors and propagate failures with `?`.
How it connects
Connects to error handling, panics, the `?` operator, the `From` trait, and Actix Web’s `Error` and `ResponseError`.

Also explained in

Compare how 8 books explain result type →

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.