Implementing the Read trait
I'm almost halfway through Ken Youens-Clark's Command-Line Rust: A Project-Based Primer for Writing Rust CLIs book, and I thought I'd take a moment to get a bit more intimidate with the BufReader.
Here's a "helloworld" which implements the Read trait, necessary for BufReader's new method and read_line (from BufRead trait):
use std::io::{BufReader, BufRead};
struct Foobar;
impl std::io::Read for Foobar {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
buf[0] = 'h' as u8;
buf[1] = 'e' as u8;
buf[2] = 'l' as u8;
buf[3] = 'l' as u8;
buf[4] = 'o' as u8;
buf[5] = 'w' as u8;
buf[6] = 'o' as u8;
buf[7] = 'r' as u8;
buf[8] = 'l' as u8;
buf[9] = 'd' as u8;
buf[10] = '\n' as u8;
Ok(11)
}
}
fn main() {
let mut br = BufReader::new(Foobar {});
let mut b = String::new();
match br.read_line(&mut b) {
Ok(_) => print!("{}", b.to_string()),
Err(e) => eprint!("{}", e)
}
}