Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provides an alternative print and println macro that don't panic. #29464

Conversation

bltavares
Copy link
Contributor

The println and print macros provides a simple interface to output
content on the stdout of a program. The macros panic when writing to
stdout, and the failure condition could happen on external conditions.

Take the following rust code:

fn main() {
  for _ in 0..10000 {
    println!("line") {
  }
}

Piping the program output to other utilities could cause the program to
panic, when the pipe is closed.

produce_logs | head
line
line
line
line
line
line
line
line
line
line
thread '<main>' panicked at 'failed printing to stdout: Broken pipe (os error 32)', ../src/libstd/io/stdio.rs:588

Instead of panicking, it would be interesting to allow the developer to
decide what to do with the error result, either ignoring it or panicking
on it's own. This commit implements try_println and try_print as an
alternative non-panicking macros.

The following code will not panic anymore when the pipe is closed.

fn main() {
  for _ in 0..10000 {
    if let Err(_) = try_println!("line") {
      std::process::exit(0);
    }
  }
}

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @nikomatsakis (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. The way Github handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

The `println` and `print` macros provides a simple interface to output
content on the stdout of a program. The macros panic when writing to
stdout, and the failure condition could happen on external conditions.

Take the following rust code:

```rust
fn main() {
  for _ in 0..10000 {
    println!("line") {
  }
}
```

Piping the program output to other utilities could cause the program to
panic, when the pipe is closed.

```bash
produce_logs | head
line
line
line
line
line
line
line
line
line
line
thread '<main>' panicked at 'failed printing to stdout: Broken pipe (os error 32)', ../src/libstd/io/stdio.rs:588
```

Instead of panicking, it would be interesting to allow the developer to
decide what to do with the error result, either ignoring it or panicking
on it's own. This commit implements `try_println` and `try_print` as an
alternative non-panicking macros.

The following code will not panic anymore when the pipe is closed.

```rust
fn main() {
  for _ in 0..10000 {
    if let Err(_) = try_println!("line") {
      std::process::exit(0);
    }
  }
}
```
@bltavares bltavares force-pushed the println-information-when-it-could-fail branch from 13113ee to fb6d310 Compare October 30, 2015 00:31
@alexcrichton
Copy link
Member

Thanks for the PR! This unfortunately must be a change to the prelude of the standard library right now as we don't have macro imports or stability, and as a result this requires an RFC to land first (to have some design discussion about this and such). This has also come up before in issues like #14505, so there may be some good historical context there!

For now, however, I'm going to close this, but if you need help writing an RFC please just let me know!

@bltavares
Copy link
Contributor Author

@alexcrichton Thanks for the feedback.
I will try to write the RFC. I will probably send it to you for comments before sending it, if that is ok. (:

@wthrowe
Copy link
Contributor

wthrowe commented Oct 31, 2015

You might be interested in the discussion in #24821 if you haven't seen it yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants