I have the following code:
use std::io::{Cursor, BufReader, Read};
fn main() {
let string = String::from("Cello");
let bytes = string.as_bytes();
let cursor = Cursor::new(bytes);
let mut reader = BufReader::new(cursor);
let mut buf: Vec<u8> = Vec::with_capacity(4);
assert_eq!(4, buf.capacity());
reader.read_exact(&mut buf);
assert_eq!(4, buf.len()); //thread 'main' panicked at 'assertion failed: `(left == right)`
//left: `4`, right: `0`'
}
According to std::io::read_exact
's documentation, it will "read the exact number of bytes required to fill buf". But in this case, despite having the capacity for 4 bytes, no bytes are read into the vec. What's going on?