# More extensive use of Result could yield safer, clearer code

**URL:** <https://internals.rust-lang.org/t/more-extensive-use-of-result-could-yield-safer-clearer-code/14022>\
**Category:** Uncategorized\
**Created:** [February 10, 2021, 1:38pm UTC](https://internals.rust-lang.org/t/more-extensive-use-of-result-could-yield-safer-clearer-code/14022 "2021-02-10T13:38:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bbarker](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bbarker/32/7519_2.png) [@bbarker](https://internals.rust-lang.org/u/bbarker)\
**Post date:** [February 10, 2021, 1:38pm UTC](https://internals.rust-lang.org/t/more-extensive-use-of-result-could-yield-safer-clearer-code/14022/1 "2021-02-10T13:38:08Z")

</div>

I'm curious about this situation:

```rust
fn main() {
    foo()
}

fn foo() -> () {
    let hello_world = "Hello, world!";
    let res = println!("{}", hello_world); 
    res
}

```

While this isn't likely to fail, I imagine there are cases where it could. Arguably, anything involving I/O could fail. It seems like requiring the signature to be `fn foo() -> Result<(), Error>` would be appropriate here. I'm not aware of any major usability issues with this, as you could always explicitly panic if needed. This also makes it more clear that yes, `foo` is doing something that might fail. If we wanted to be clear it was I/O related we could introduce a type alias to `Result<(), Error>` for clarity, but I already think making the more fundamental change would be enough for most.

I'm sure there are other instances like this, not just `println!`, but it is the one I've seen the most.

---

<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:** [February 10, 2021, 2:21pm UTC](https://internals.rust-lang.org/t/more-extensive-use-of-result-could-yield-safer-clearer-code/14022/2 "2021-02-10T14:21:41Z")

</div>

You can opt-in to fallible `stdout` writing already

```rust
use std::io::{self, Write};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    foo()?;
    Ok(())
}

fn foo() -> io::Result<()> {
    let hello_world = "Hello, world!";
    writeln!(io::stdout(), "{}", hello_world)?;
    Ok(())
}

```

`println!` is a convenience macro for use in cases where you always expect `stdout` will be writeable, more serious applications should be using logging libraries and `writeln!(stdout` to detect error conditions (and also `Stdout::lock` for better performance).

---

<div class="post-metadata">

**Author:** ![Lonami](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/lonami/32/6774_2.png) [@Lonami](https://internals.rust-lang.org/u/Lonami)\
**Post date:** [February 10, 2021, 9:54pm UTC](https://internals.rust-lang.org/t/more-extensive-use-of-result-could-yield-safer-clearer-code/14022/3 "2021-02-10T21:54:44Z")

</div>

And if you're logging things, consider printing to `stderr` instead with `eprintln!();` if you don't want to pull a dependency. Stdout should only be for actual output that another program might want to consume, stderr is for all logging things related (error reporting, progress, 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:** [February 11, 2021, 12:58am UTC](https://internals.rust-lang.org/t/more-extensive-use-of-result-could-yield-safer-clearer-code/14022/4 "2021-02-11T00:58:44Z")

</div>

Doccy side note: Some variations on `println!` are mentioned [in the docs](https://doc.rust-lang.org/std/macro.println.html), but no mention is made of `writeln!` or Results. Which is all to say, some of this discussion could be added to the docs.

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [February 11, 2021, 3:47am UTC](https://internals.rust-lang.org/t/more-extensive-use-of-result-could-yield-safer-clearer-code/14022/5 "2021-02-11T03:47:18Z")

</div>

You can fix it!

> <https://github.com/rust-lang/rust/blob/e0d9f793990d20f8f640097e28556886ba5362f0/library/std/src/macros.rs#L52>

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [May 12, 2021, 3:47am UTC](https://internals.rust-lang.org/t/more-extensive-use-of-result-could-yield-safer-clearer-code/14022/6 "2021-05-12T03:47:34Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
