Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created November 26, 2021 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/4e0c835829b429a8d78f7315e1473a6b to your computer and use it in GitHub Desktop.
Save rust-play/4e0c835829b429a8d78f7315e1473a6b to your computer and use it in GitHub Desktop.
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