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

**URL:** <https://internals.rust-lang.org/t/if-cond-return-ret-vs-ensure-cond-ret/17251>\
**Category:** language design\
**Created:** [August 24, 2022, 11:09am UTC](https://internals.rust-lang.org/t/if-cond-return-ret-vs-ensure-cond-ret/17251 "2022-08-24T11:09:46Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [August 24, 2022, 5:13pm UTC](https://internals.rust-lang.org/t/if-cond-return-ret-vs-ensure-cond-ret/17251/9 "2022-08-24T17:13:04Z")

</div>

> [@kornel](#):
>
> I'd prefer syntax sugar for this case. Maybe `foo_or_bar.get_foo().or("fail")?`.

In C#, this has started to be covered by the [?? and ??= operators - null-coalescing operators - C# | Microsoft Learn](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator).

For example, in C# you'll see

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

```

We could certainly have something like

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

```

in rust to simplify this.

A previous thread: [Something for coalescing; aka generalized/improved `or_else`](https://internals.rust-lang.org/t/something-for-coalescing-aka-generalized-improved-or-else/9295)

Note that, for bool, one _can_ do this:

```rust
b || do yeet "uhoh";

```

[https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=e04f964455a0ea16b3d2f3b76d87486a](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.

---

_[View the full topic](https://internals.rust-lang.org/t/if-cond-return-ret-vs-ensure-cond-ret/17251)._
