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


#1

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.


#2

Neat idea! Thanks for sharing it.


#3

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


#4

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.


#5

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


#6

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


#7

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


#8

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


#9

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


#10

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


#11

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


#12

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.


#13

@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?


#14

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.