io::Write::write()
and io::Read::read()
are not guaranteed to process the entire buffer.
They return how many bytes were processed, which might be smaller than a given buffer’s length.
If you don’t need to deal with partial-write/read, use write_all()
/read_exact()
instead.
use std::io;
fn foo<W: io::Write>(w: &mut W) -> io::Result<()> {
w.write(b"foo")?;
Ok(())
}
fn bar<R: io::Read>(r: &mut R) -> io::Result<()> {
r.read("baz".to_bytes())?;
Ok(())
}
use std::io;
fn foo<W: io::Write>(w: &mut W) -> io::Result<()> {
w.write_all(b"foo")?;
Ok(())
}
fn bar<R: io::Read>(r: &mut R) -> io::Result<()> {
r.read_exact("baz".to_bytes())?;
Ok(())
}