0

What's the functional approach to replace the below match?

match second_db.player_create(player_id).await {
    Ok(o) => Ok(o),
    
    Err(err) => {
        first_db.player_delete(player_id).await?;

        Err(err)
    }
}
Fred Hors
  • 3,258
  • 3
  • 25
  • 71
  • 1
    What do you mean by "functional approach"? Match statements are usually considered to be quite a functional pattern. – jthulhu Oct 04 '22 at 16:01
  • I don't think it could be reduced any further, because you return a value from inside of your match statement. Also you have bizarre control flow. Why do you want to delete a player when it's creation failed? And why you return error to the caller when it fails? It seems to me that you have a problem elsewhere. – Aleksander Krauze Oct 04 '22 at 16:15
  • 1
    If you're looking for an approach with chained methods, there is `.or_else()` to chain a failed result into a fallible lambda (much like `.and_then()` for the successful case). However, you won't be able to use that here since those methods are not async-agnostic. What you have is probably the best you're going to get. – kmdreko Oct 04 '22 at 16:17

1 Answers1

1

As others have pointed out, the usual helper methods on Result and Option do not work with async (see links below). However, these methods are mostly about transforming the Result / Option which you are not doing. Hence, your code could be rephrased to the following (assuming you want to return the result of the match expression):

let result = second_db.player_create(player_id).await;
if result.is_err() {
    first_db.player_delete(player_id).await?;
}
result

This omits the "mapping" part of both branches and is, in my opinion, easier to understand.


Related discussions:

Niklas Mohrin
  • 1,756
  • 7
  • 23