Mastodon Feed: Post

Mastodon Feed

rust@octodon.social ("Rust tips") wrote:

Do you want io::Read::read_exact(), but read into a Vec instead?

let bytes\_read = reader  
    .by\_ref() // optional: avoids consuming the reader  
    .take(length\_to\_read) // limits number of bytes  
    .read\_to\_end(&mut vec)?; // reads up to the limit  
// the file could have ended sooner, so needs a check  
if bytes\_read != length\_to\_read { return Err("EOF") }

https://play.rust-lang.org/?gist=05ac9c2145aa61b3442359921dd0607d