# \[Pre-RFC\] \`Result::void()\` and \`Option::void()\`

**URL:** <https://internals.rust-lang.org/t/pre-rfc-result-void-and-option-void/14947>\
**Category:** language design\
**Created:** [June 29, 2021, 4:39pm UTC](https://internals.rust-lang.org/t/pre-rfc-result-void-and-option-void/14947 "2021-06-29T16:39:34Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![Nemo157](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nemo157/32/11585_2.png) [@Nemo157](https://internals.rust-lang.org/u/Nemo157)\
**Post date:** [July 3, 2021, 7:12am UTC](https://internals.rust-lang.org/t/pre-rfc-result-void-and-option-void/14947/22 "2021-07-03T07:12:32Z")

</div>

> [@fosskers](#):
>
> The only two contexts (re: Monads) we care about in Rust are `Option` and `Result` .

`Iterator` and `Future` are also very cared about monads, the latter has a whole language syntax dedicated to it, and adding some syntax to simplify the former has been widely discussed.

---

<div class="post-metadata">

**Author:** ![RalfJung](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ralfjung/32/2415_2.png) [@RalfJung](https://internals.rust-lang.org/u/RalfJung)\
**Post date:** [July 3, 2021, 8:10am UTC](https://internals.rust-lang.org/t/pre-rfc-result-void-and-option-void/14947/23 "2021-07-03T08:10:38Z")

</div>

> [@phlopsi](#):
>
> When I read the title, I assumed either something related to C FFI or the never type.

C `void` corresponds to Rust `()`, so that was my first association. 😉 The never type does not really fit. (But sadly, C `void` is often described as "containing no value", which of course is wrong as we can easily see in Rust where we have the value `()` inhabiting the type `()`.)

---

<div class="post-metadata">

**Author:** ![fosskers](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/fosskers/32/8310_2.png) [@fosskers](https://internals.rust-lang.org/u/fosskers)\
**Post date:** [July 3, 2021, 2:50pm UTC](https://internals.rust-lang.org/t/pre-rfc-result-void-and-option-void/14947/24 "2021-07-03T14:50:37Z")

</div>

The naming is purely historical: I called it `void` here because that's the symbol name in Haskell and Scala (at least).

---

<div class="post-metadata">

**Author:** ![marcianx](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/marcianx/32/3249_2.png) [@marcianx](https://internals.rust-lang.org/u/marcianx)\
**Post date:** [July 3, 2021, 11:07pm UTC](https://internals.rust-lang.org/t/pre-rfc-result-void-and-option-void/14947/25 "2021-07-03T23:07:11Z")

</div>

Sounds like you want [fehler](https://github.com/withoutboats/fehler)?

---

<div class="post-metadata">

**Author:** ![fosskers](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/fosskers/32/8310_2.png) [@fosskers](https://internals.rust-lang.org/u/fosskers)\
**Post date:** [July 3, 2021, 11:21pm UTC](https://internals.rust-lang.org/t/pre-rfc-result-void-and-option-void/14947/26 "2021-07-03T23:21:08Z")

</div>

Thanks for the suggestion, but the main point of `void` is to "drop to `()`", which is useful in a number of places. All the discussion involving error handling has to do with a `void` call being made as the last line of function.

---

<div class="post-metadata">

**Author:** ![marcianx](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/marcianx/32/3249_2.png) [@marcianx](https://internals.rust-lang.org/u/marcianx)\
**Post date:** [July 3, 2021, 11:37pm UTC](https://internals.rust-lang.org/t/pre-rfc-result-void-and-option-void/14947/27 "2021-07-03T23:37:15Z")

</div>

That's what a semicolon is for. E.g. with fehler, your original example becomes:

```rust
#[throws(Error)]
fn foo() {
    let a = bar()?;
    let b = baz()?;
    work(a, b)?;
}

```

---

<div class="post-metadata">

**Author:** ![fosskers](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/fosskers/32/8310_2.png) [@fosskers](https://internals.rust-lang.org/u/fosskers)\
**Post date:** [July 3, 2021, 11:42pm UTC](https://internals.rust-lang.org/t/pre-rfc-result-void-and-option-void/14947/28 "2021-07-03T23:42:22Z")

</div>

Sorry, please reread the original posting and comments.

---

<div class="post-metadata">

**Author:** ![josh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/josh/32/5934_2.png) [@josh](https://internals.rust-lang.org/u/josh)\
**Post date:** [July 7, 2021, 3:12am UTC](https://internals.rust-lang.org/t/pre-rfc-result-void-and-option-void/14947/29 "2021-07-07T03:12:15Z")

</div>

> [@fosskers](#):
>
> Sorry, please reread the original posting and comments.

Please don't assume that people who disagree with you haven't read what you've written.

I've read your entire original post, and I don't see the use case for `.void()`. For `Option` it never seems useful (`Option<()>` is rarely a useful type) and for `Result` it seems like a niche helper that might be helpful for the occasional closure argument but not sufficiently common to put in the standard library.

---

<div class="post-metadata">

**Author:** ![mbrubeck](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mbrubeck/32/174_2.png) [@mbrubeck](https://internals.rust-lang.org/u/mbrubeck)\
**Post date:** [July 7, 2021, 3:23am UTC](https://internals.rust-lang.org/t/pre-rfc-result-void-and-option-void/14947/30 "2021-07-07T03:23:02Z")

</div>

For what it's worth, here are your other examples rewritten to use `fehler`. Note that `fehler` has the same advantages as `try` blocks but without the disadvantage of additional nesting.

```rust
#[throws(Error)]
pub(crate) fn search(path: &Path, term: String) {
    let (search, args) = misc::searcher();
    Command::new(search)
        .args(args)
        .arg(term)
        .arg(path)
        .status()?;
}

```

```rust
/// Open a given URL in a browser.
#[throws(std::io::Error)]
pub(crate) fn open(url: &str) {
    webbrowser::open(url)?;
}

```

```rust
#[throws(&str)]
fn clone(p: &String) {
    let pkg = p.as_str();
    aura!(fll, "A-w", package = pkg);
    clone_aur_repo(None, &p).ok_or(pkg)?;
}
let clones: Validated<(), &str> = packages.par_iter().map(clone).collect();

```

---

<div class="post-metadata">

**Author:** ![fosskers](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/fosskers/32/8310_2.png) [@fosskers](https://internals.rust-lang.org/u/fosskers)\
**Post date:** [July 7, 2021, 3:23am UTC](https://internals.rust-lang.org/t/pre-rfc-result-void-and-option-void/14947/31 "2021-07-07T03:23:59Z")

</div>

Everyone's allowed to disagree, but it seemed to me that he hadn't understood the original motivation and discussion.

---

<div class="post-metadata">

**Author:** ![fosskers](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/fosskers/32/8310_2.png) [@fosskers](https://internals.rust-lang.org/u/fosskers)\
**Post date:** [July 7, 2021, 3:26am UTC](https://internals.rust-lang.org/t/pre-rfc-result-void-and-option-void/14947/32 "2021-07-07T03:26:39Z")

</div>

Thank you for those examples, I can see the appeal. Personally I wouldn't choose to rely on macros here though. My gut feeling is that the function signature should be the source-of-truth-at-a-glance.

---

<div class="post-metadata">

**Author:** ![Soni](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@Soni](https://internals.rust-lang.org/u/Soni)\
**Post date:** [July 7, 2021, 11:10am UTC](https://internals.rust-lang.org/t/pre-rfc-result-void-and-option-void/14947/33 "2021-07-07T11:10:17Z")

</div>

What about using `use drop as void;` and `thing.map(void).map_err(From::from)`? You also get more control on whether to use From or not use From with this.

---

<div class="post-metadata">

**Author:** ![fosskers](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/fosskers/32/8310_2.png) [@fosskers](https://internals.rust-lang.org/u/fosskers)\
**Post date:** [July 7, 2021, 4:59pm UTC](https://internals.rust-lang.org/t/pre-rfc-result-void-and-option-void/14947/34 "2021-07-07T16:59:26Z")

</div>

For sure this is an existing possibility. If we consider that to be a "common capturable pattern", then introducing a shorthand like `void` could make sense (at least, that was my original motivation). Plenty such one-liner patterns exist as convenience methods in `std`. And if given an official method, it could be documented, tested, etc etc.

---

<div class="post-metadata">

**Author:** ![toc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/toc/32/6692_2.png) [@toc](https://internals.rust-lang.org/u/toc)\
**Post date:** [July 7, 2021, 8:55pm UTC](https://internals.rust-lang.org/t/pre-rfc-result-void-and-option-void/14947/35 "2021-07-07T20:55:04Z")

</div>

> [@fosskers](#):
>
> Thank you for those examples, I can see the appeal. Personally I wouldn't choose to rely on macros here though. My gut feeling is that the function signature should be the source-of-truth-at-a-glance.

I don't think anyone disagrees overly with that last part. Don't think of the macros as a crutch, in this case they are very directly being used to explore the problem space of error handling. Error handling is a super important part of any language's story and error handling in Rust has definitely not achieved its final form.

1. Are the error handling macros super useful and general and commonly used (e.g. `dbg!`)? -\> Let's move them to std!
2. Are the macros super useful, but expose some ugly corner cases that could be easily fixed by language level changes? Error handling is an important use case for sure. -\> Let's consider changes to the language that could make this story better.
3. What about outstanding changes to the language, like `try`?

The throws macro seems to do most of what you're asking for with a slightly different syntax.

By the way with `anyhow` I would write your example as

```rust
use anyhow::Result;

fn foo() -> Result<()> {
    let a = bar()?;
    let b = baz()?;
    work(a, b)?;
    Ok(())
}

```

which actually seems pretty close to using `.void()`.

---

<div class="post-metadata">

**Author:** ![fosskers](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/fosskers/32/8310_2.png) [@fosskers](https://internals.rust-lang.org/u/fosskers)\
**Post date:** [July 7, 2021, 9:20pm UTC](https://internals.rust-lang.org/t/pre-rfc-result-void-and-option-void/14947/36 "2021-07-07T21:20:19Z")

</div>

My dream is to do away with the `Ok(())` here, which I'm arguing is smell. I actually like `anyhow` and use it in a number of crates. You're right that Rust as a whole hasn't reached its final answer w.r.t. Error handling, so I'm glad we can have these discussion and evolve the language proactively.

Error handling aside, is there any strong objection to a canonical `fn void(self) -> Result<(), E>` for the purposes of ignoring return values as described in the original posting (i.e. in lambdas or for `Command::status`)?

---

<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:** [July 7, 2021, 10:25pm UTC](https://internals.rust-lang.org/t/pre-rfc-result-void-and-option-void/14947/37 "2021-07-07T22:25:45Z")

</div>

> [@fosskers](#):
>
> My dream is to do away with the `Ok(())` here, which I'm arguing is smell.

You will find many, many people who consider it valuable, and not at all a smell. See all the threads about `try fn`, or even just whether `try` blocks should success-wrap the body of the block.

`?; Ok(())` vs `.void()` just doesn't seem that valuable, especially since it's so specific to `()`.

---

<div class="post-metadata">

**Author:** ![marcianx](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/marcianx/32/3249_2.png) [@marcianx](https://internals.rust-lang.org/u/marcianx)\
**Post date:** [July 8, 2021, 2:04am UTC](https://internals.rust-lang.org/t/pre-rfc-result-void-and-option-void/14947/38 "2021-07-08T02:04:46Z")

</div>

> [@fosskers](#):
>
> Everyone's allowed to disagree, but it seemed to me that he hadn't understood the original motivation and discussion.

FWIW, I believe I did understand it completely, but, I didn't think this misunderstanding was worth derailing the topic.

But to be sure, I did reread the thread immediately after your response, and the one nuance I noticed was that your `let clones` example would have benefited from `fehler` supporting closures, which it doesn't seem to currently. I hadn't responded immediately to avoid being reactive, but then life caught up with me and I never got back to it (well, until now, I guess 😃).

**Edit:** Also, while I do prefer not going out of my way just to avoid `Ok(())` and I'm unlikely to use `fehler` myself, I do recognize that there are others in the opposite camp, [including the author of `fehler`](https://boats.gitlab.io/blog/post/failure-to-fehler/). That's actually the principal reason it occurred to me to point his work out.

In a similar vein, you too could publish a crate that implements `void()` for all `Result` types using an extension method and see how broadly useful it is. Actual use may help motivate your proposal more convincingly.

---

<div class="post-metadata">

**Author:** ![skysch](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@skysch](https://internals.rust-lang.org/u/skysch)\
**Post date:** [July 8, 2021, 2:59am UTC](https://internals.rust-lang.org/t/pre-rfc-result-void-and-option-void/14947/39 "2021-07-08T02:59:18Z")

</div>

The word "smell" usually refers to code patterns that are indicative of a design error or programming risk. It doesn't typically refer to perfectly common, well understood, safe constructs that we just happen to not like the spelling of.

---

<div class="post-metadata">

**Author:** ![fosskers](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/fosskers/32/8310_2.png) [@fosskers](https://internals.rust-lang.org/u/fosskers)\
**Post date:** [July 8, 2021, 5:40am UTC](https://internals.rust-lang.org/t/pre-rfc-result-void-and-option-void/14947/40 "2021-07-08T05:40:03Z")

</div>

> [@marcianx](#):
>
> I hadn't responded immediately to avoid being reactive, but then life caught up with me and I never got back to it

Pardon me if my initial response seemed flippant, that wasn't my intention. I probably also misunderstood your replies myself. Thanks for circling back to this.

re: `fehler`. I see now that I does a decent job at addressing my woes concerning `Ok(())`, etc., if I'm willing to accept macros and indirection w.r.t. the at-a-glance return type in the function signature.

> [@marcianx](#):
>
> you too could publish a crate that implements `void()` for all `Result` types

I have actually done just that (minus a separate crate). The `ResultVoid` trait I showed in the original post (and the rest of the examples, for that matter) are lifted from a real-world project. But forcing an extra trait import on the user just to get access to `void` seems icky too, hence I'm here proposing its direct addition to `std`.

---

<div class="post-metadata">

**Author:** ![fosskers](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/fosskers/32/8310_2.png) [@fosskers](https://internals.rust-lang.org/u/fosskers)\
**Post date:** [July 8, 2021, 5:46am UTC](https://internals.rust-lang.org/t/pre-rfc-result-void-and-option-void/14947/41 "2021-07-08T05:46:35Z")

</div>

Thanks. I had mentioned in the original posting that a function-final `Ok(())` in Rust is equivalent to a function-final `pure ()` in Haskell, which is universally given a lint warning. In both cases, type-wise, such a line is strictly unnecessary, whatever other benefit it may have (hence my diagnosis; I claim that this isn't merely a matter of personal taste). If we're collectively engaged in the pursuit of detecting common patterns and abstracting them away, then this seems to me to be fertile ground for exactly that. `fehler` does seem to address this particular point well, now that I look closer, but it has its costs.

[Previous page](https://internals.rust-lang.org/t/pre-rfc-result-void-and-option-void/14947.md?page=1)

[Next page](https://internals.rust-lang.org/t/pre-rfc-result-void-and-option-void/14947.md?page=3)
