A suggestion to add a write_line(&str) function to the BufWriter trait as a corollary to the BufReader.read_line() function. It would append the contents of the &str followed by 0xA to the underlying Write object.
The trait is Write, BufWriter is just a struct that implements it.
This is not symmetric: There is a BufRead trait as well as a Read trait, both implemented by BufReader. The reason for this is that BufRead offers useful functionality that is hard to implement by hand: read_line() is hard to implement with traditional IO APIs because without a buffer on your side you have to read one byte at a time, which is of course very slow (if you read more, you have nowhere to put the data after the linebreak).
There is no similar motivation for write_line. You just write your data, it just happens to be terminated with a linebreak.
While BufReader implements very useful functionality that's impossible without a read buffer, BufWriter is only a small performance optimization (to save syscalls, kernel still buffers your writes by default) and nothing more.
The writeln!() macro does what you want (append a newline). And if you wrap your stream in a BufWriter, the write probably gets buffered together with the linebreak in which case the behavior you get is 100% identical to what your proposed write_line function would do.
Doesn't seem this will lead anywhere so I'll close this. Feel free to reraise it on http://internals.rust-lang.org/ if you think it can go somewhere.
Most helpful comment
The trait is
Write,BufWriteris just a struct that implements it.This is not symmetric: There is a
BufReadtrait as well as aReadtrait, both implemented byBufReader. The reason for this is thatBufReadoffers useful functionality that is hard to implement by hand:read_line()is hard to implement with traditional IO APIs because without a buffer on your side you have to read one byte at a time, which is of course very slow (if you read more, you have nowhere to put the data after the linebreak).There is no similar motivation for
write_line. You just write your data, it just happens to be terminated with a linebreak.While
BufReaderimplements very useful functionality that's impossible without a read buffer,BufWriteris only a small performance optimization (to save syscalls, kernel still buffers your writes by default) and nothing more.The
writeln!()macro does what you want (append a newline). And if you wrap your stream in aBufWriter, the write probably gets buffered together with the linebreak in which case the behavior you get is 100% identical to what your proposedwrite_linefunction would do.