-
-
Save rust-play/4e0c835829b429a8d78f7315e1473a6b to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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