You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
use std::io::Write;use std::io;fnmain(){let stdout = io::stdout();letmut stdout = stdout.lock();let res = writeln!(&mut stdout,"Hello, World!");match res {Ok(_) => eprintln!("success"),Err(_) => eprintln!("failure")}}
I ran it with a closed stdout file descriptor from bash:
rustc test.rs
./test >&-
I expected to see this happen:
The program should print "failure" to stderr (and nothing to stdout).
Note that this behavior is actually what happens in other programs, such as /usr/bin/echo:
Instead, this happened:
The program prints "success" to stderr. Note that the writeln does in fact not succeed as the "Hello, World!" is not visible to the user on the terminal.
In my opinion this behavior is a bug since the Result of the writeln is Ok even though the user does not see the text.
Although it is somewhat of an edge case, other programs such as echo seem to be able to correctly detect this and report an error.
Thank you. Ah, OK. I see. Then I will close this issue.
I think this is unfortunate, though. Maybe it would make sense to make this configurable or to have the macros that do not return a result such as println ignore this kind of error and only report it in write calls/macros.
@38 seems to me this is more of a side-effect of the RFC's implementation detail: the RFC only mentions output methods which do not return results: println!, print!, panic!, and assert! don't return a Result and will panic on IO errors. Making them return IO errors was judged inconvenient at the time, and thus them ignoring IO errors was more practical and convenient.
That's specifically not the case for write! though, or any explicit method of the Write traits. Getting proper error reporting is pretty much the entire point of using write! with standard streams, otherwise [e]print[ln]! is a strictly terser way to do the same thing.
I tried this code:
I ran it with a closed
stdout
file descriptor from bash:rustc test.rs ./test >&-
I expected to see this happen:
The program should print "
failure
" tostderr
(and nothing tostdout
).Note that this behavior is actually what happens in other programs, such as
/usr/bin/echo
:Instead, this happened:
The program prints "
success
" tostderr
. Note that thewriteln
does in fact not succeed as the "Hello, World!" is not visible to the user on the terminal.In my opinion this behavior is a bug since the
Result
of thewriteln
isOk
even though the user does not see the text.Although it is somewhat of an edge case, other programs such as
echo
seem to be able to correctly detect this and report an error.Meta
rustc --version --verbose
:The text was updated successfully, but these errors were encountered: