Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bltavares/try_print
https://github.com/bltavares/try_print
Last synced: 28 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/bltavares/try_print
- Owner: bltavares
- Created: 2015-10-31T14:41:51.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2015-10-31T14:54:15.000Z (about 9 years ago)
- Last Synced: 2024-09-15T02:47:08.136Z (about 2 months ago)
- Language: Rust
- Size: 121 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# try_print and try_println
## Non-panicking printing to stdoutThis create provides an alternative print and println macro that don't panic.
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 '' 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 crate provides `try_println` and `try_print` as an
alternative non-panicking macros.The following code will not panic anymore when the pipe is closed.
```rust
#[macro_use] extern crate try_print;fn main() {
for _ in 0..10000 {
if let Err(_) = try_println!("line") {
std::process::exit(0);
}
}
}
```