Unwrap in libraries


#41

You can reduce the rightward drift by using the combinator functions on Option with some line breaks (rustfmt will do them for you), something like:

let opt = ...;
opt.and_then(...)
   .map(...)
   .unwrap_or(...)

It also has a nice visual pattern to it where you can see the steps taken in the transformations.


#42

I would argue, that returns from middle of function are not nice either.


#43

Yup, which is why refutable let is being talked about over on internals again as a potential solution


#44

I would argue, that returns from middle of function are not nice either.

How so?

There are countless “get this return value” processes or algorithms which are inherently a sequence of fallible steps.

You’re going to tie yourself in knots trying to implement such an algorithm without having either a pyramid of if lets, a sequence of ?s or some other construct where the control flow can return after progressing only half-way through the function.

Early return is simply an inherent property of certain algorithms and the combinator functions on Result and Option aren’t always the most maintainble way to write things.


#45

There is map, and_then, or_else on Option. There is ? operator.
This covers like 90% of cases. Another 9% are covered by properly structuring the function.
For remaining 1% you have return in middle of the function :wink:


#46

Oh. When you said “returns from” rather than “returns in”, I thought you were talking about the abstract pattern (of which ? is an example and the Option combinators optimize to) rather than the return keyword itself.