Which Option/Result combinator should I use? a helping hand

There are 27 different methods to convert between Option/Result types and their contained types. Which one should I use?

I ask myself this question a lot, so I made a selection tool for Rust combinators!

You have to input your question like so:

I have in-type and I want out-type by ok-conversion or err-conversion.

For example,

I have Option<T> and I want Option<U> by running a closure turning T into Option<U> or propagating None

You want: Option::and_then

It works well enough right now, but I'm not a UI designer. If anyone with leet CSS/JS skills wants to make it look nicer, I'll gladly except your PR.

13 Likes

Neat idea! Thanks for sharing it.

There is also a useful Vec<Result<T, E>> -> Result<Vec<T>, E> transformation via collect

Oh, sweet! I'm wondering how to best represent this... I have Iterator<Result<T,E>> and I want Result<Collection<T>,E> by collecting the result and propagating the first error? And then link to Iterator::collect? Samething for Option btw.

Seems a good idea to have a centralized place to aggregate these nice efforts?

I like this! It also makes it clear which corners are missing. Eg, I wanted Result::unwrap_err_or() a few days ago...

http://forge.rust-lang.org is a place intended for things like that.

2 Likes

Update: Added Option::filter, Result::unwrap_or_default, and Result::expect_err.

1 Like

Shall Future<T> be in scope of this project?

Sure, I think that makes sense! @vi0 Do you want to take a stab at it?

Wow, this is really helpful to a rust newbie like me.

Your page falls foul of the Content Security Policy (CSP), with the following error message:

Content Security Policy: The page’s settings blocked the loading of a resource at inline (“script-src”). rust-combinators:37:1

When I security.csp.enable to false it works.

This is Firefox 63.0.3 (64-bit) on Linux. security.csp.enable is set to true by default. In fact, all settings with CSP as part of their name are set to their default value. I do block cookies and use uMatrix and uBlock origin, but I don't think this is relevant for this issue.

@starblue Feel free to file a bug report on GitHub.

However, I use that exact same browser on Linux with CSP enabled and have no issues. Since you are known to have a very unusual browsing setup, can you please check all your settings/plugins/extensions/etc. against a standard installation before reporting that something is broken?

You should view the error reports as a compliment, your site is important enough to care. Obviously there's a limit to how much effort I can put into that up front.

That said, it seems to be fixed now, without any change on my side.
Thanks to whoever did that.