Result.map to error type

#1

Hi,

I’m a little new to rust and have been trying to map Result.success type to a Result.error type:

// Lets say conn.execute returns Result<u32, ErrorT>
conn.execute("<some update sql>")
.map(|rows_changed: u32| {
    if rows_changed != 1 {
        return ErrorT { ... }
    } else {
        return ();
    }
})

I know I can just unwrap the result, perform my logic then re-assemble into a new Result, but it feels like i’m breaking up the async flow

I am already using the function map_err, to map DbError type to HttpError, but I also want to conditionally map u32 to HttpError

Is there a way too solve this?

Thanks!

#2

If it’s the same error type then you can use Result.and_then

conn.execute("<some update sql>")
    .and_then(|rows_changed: u32| {
        if rows_changed != 1 {
            return Err(ErrorT { ... });
        } else {
            return Ok(());
        }
    })

btw there is no async flow you can break.

#3

Thanks, that worked!

Ahh for some reason I thought result was behaving like a future, no idea how I came to that conclusion