Unwrap in libraries


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 = ...;

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


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


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


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.


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:


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.