nunwrap!

nunwrap (None unwrap) is a macro made to help you stop nesting code when using Option<_>.

You can think of this macro sort of like if let None(val) return backup;.

It tests if the variable is None, if it is it runs your expression (usually returning), otherwise the variable will be reassigned to the unwrapped value automatically.

Usage

The goal is to return/break/continue from the scope we're in, so a block/unwrap_or isn't usable here.

Here return is used, but any expression can be used such as break or continue.

```rs // Bad. // This can quickly get out of control when nesting many of them. if let Some(somevar) = something.some_option() { // ... } return backup;

// Better. // This works, but it takes up a lot of lines. let somevar = something.someoption(); let None = somevar { return backup; } let somevar = something.unwrap();

// Best. // nunwrap shortens the visible code immensely and prevents nesting. nunwrap!(let somevar = something.some_option(); or return backup); ```

nunwrap also supports mut variables and checking existing variables.

```rs // Assigns to mutable and checks. nunwrap!(let mut somevar = something.some_option(); or return backup);

// Checks and unwraps to immutable. nunwrap!(let some_var; or return backup);

// Checks and unwraps to mutable. nunwrap!(let mut some_var; or return backup); ```

It also tries to use the most efficient way of moving around your value which may differ depending on what you're doing.

Types

If needed, you can also specify the type just like you would when you assign a variable normally.

rs nunwrap!(let mut some_var: usize = some_thing.some_option(); or return backup);

Identifier Types

One minor "caveat" is that when you pass an identifier (variable name), the compiler can't infer the type of it unless you specify the type in the macro.

This means a shortcut that removes a variable declaration can't be done.

So in order to save the precious nugatory time that's wasted, if you really want to, specify the type.

```rs // Blazingly slow!!!!! nunwrap!(let mut somevar = variablename; or return backup);

// Saves literally 2 picoseconds!!!!! nunwrap!(let mut somevar: usize = variablename; or return backup); ```