`if !cond { return ret; }` vs `ensure!(cond, ret)`

In C#, this has started to be covered by the ?? and ??= operators - null-coalescing operators - C# | Microsoft Learn.

For example, in C# you'll see

this.foo = foo ?? throw new ArgumentNullException(nameof(foo));

We could certainly have something like

let value = array.get(i) ☃ yeet "Err";

in rust to simplify this.

A previous thread: Something for coalescing; aka generalized/improved `or_else`

Note that, for bool, one can do this:

b || do yeet "uhoh";

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=e04f964455a0ea16b3d2f3b76d87486a

And it'll work great, though the linter doesn't like it.

1 Like