Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created Nov 26, 2021
Embed
What would you like to do?
Code shared from the Rust Playground
use std::io::{self, Write};
#[derive(Default)]
struct Alpha {
buf: Vec<u8>,
}
impl Write for Alpha {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.buf.extend_from_slice(buf);
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
println!(
"[FLUSH] {}",
String::from_utf8(self.buf.drain(..).collect()).unwrap()
);
Ok(())
}
}
fn main() {
let mut writer = io::LineWriter::new(Alpha::default());
for _ in 0..5 {
writeln!(&mut writer, "Hello, world!").unwrap();
}
println!("Ok");
writer.flush().unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment