Skip to content

Instantly share code, notes, and snippets.

@tos-kamiya
Last active February 25, 2020 20:33
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 tos-kamiya/e25c2aed58f6f203c4d4b32ed8f3c2f1 to your computer and use it in GitHub Desktop.
Save tos-kamiya/e25c2aed58f6f203c4d4b32ed8f3c2f1 to your computer and use it in GitHub Desktop.
Rust's Cursor, Python's StringIO like object
use std::io;
use std::io::{ Write, Seek, BufRead };
fn main() {
let mut buf = io::Cursor::new(Vec::<u8>::new());
writeln!(buf, "{}", "abc").unwrap();
writeln!(buf, "{}", "def").unwrap();
buf.seek(io::SeekFrom::Start(0)).unwrap();
let mut line = String::new();
buf.read_line(&mut line).unwrap();
println!("{}", line);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment