Chaining Options and Results

simply, I need to run a number of transformations, each of which may return Option or Result - is there an elegant way of chaining them together? I thought and_then would work but it gave weird type errors.

For example:

(I'll put together a rust playground snippet if that would be helpful?)

actually, the type errors might be more to do with the error types rather than the happy path types. I think I need to play around a little more...

The answer in these cases is usually something with ?:

For example, something like let uuid = Uuid::parse_str(s).map_err(|_| (StatusCode::BAD_REQUEST, "XYZ malformed"))?;.

(Also if you post code as text it makes it easier for people to copy-paste.)

It's very helpful, as it gives us play around with refactoring to show the options. (Like I recently did in Reducing the friction in the API for converting from Option to Result - #5 by scottmcm, for example.)

Either one that compiles but you wish was more elegant, or one that shows the same error you're getting is a great way to get help surprisingly quickly here, and many members of the forum like the puzzles :slight_smile:


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.