Idiomatic way to return/break if Err/None

This page summarizes the projects mentioned and recommended in the original post on /r/rust

InfluxDB - Power Real-Time Data Analytics at Scale
Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.
www.influxdata.com
featured
SaaSHub - Software Alternatives and Reviews
SaaSHub helps you find the best software and product alternatives
www.saashub.com
featured
  • rust

    Empowering everyone to build reliable and efficient software.

  • Looks good to me. There is some work on simplifying this “extract value or diverge” pattern (let…else, RFC 3137), but that’s still some ways away. You can also use the ? operator to return from the function early. There is also ongoing work on try blocks which allow reducing the scope of error propagation (RFC 243), but again, I don’t expect this to hit stable soon.

  • thiserror

    derive(Error) for struct and enum error types

  • The idiomatic way of doing this is making sure you always return results with error types that are convertible. How do you do this? One way is to use thiserror. You define your library's error type using it and you wrap lower error types in it, add from's using the macro, etc. This way you avoid all the cumbersome conversion and just let the ? operator or a map handle it for you.

  • InfluxDB

    Power Real-Time Data Analytics at Scale. Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.

    InfluxDB logo
  • anyhow

    Flexible concrete Error type built on std::error::Error

  • Alternatively, if you've got a lot of error types and you're outside a library (so directly in a binary where you don't plan to reuse code elsewhere) you can use anyhow. This gives you an error type you can basically propagate any other error through. On top of that you can attach context information at every return. It's basically a more complicated Result>.

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts