Last active
December 17, 2020 15:09
-
-
Save arnabanimesh/b9eaaf43ed479ec681ac732d1ece0974 to your computer and use it in GitHub Desktop.
Multiline rust input with known and unknown number of lines
This file contains hidden or 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
//https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=9cb93f0342793c225be94aa46897c25a | |
use std::io::{Cursor, BufReader}; | |
// for console input replace the above line with this | |
// use std::io::stdin; | |
fn main() { | |
// simulate stdin in rust playground | |
let f = Cursor::new("3\n40 3\n30 4\n20 5".to_string()); | |
let input = BufReader::new(f); | |
let mut vec = input.lines(); | |
// for console input replace the above lines with these | |
// let input = stdin(); | |
// let mut vec = input.lock().lines(); | |
let n = vec.next().unwrap().unwrap().parse::<usize>().unwrap(); | |
let mut ans = String::with_capacity(n * 5); | |
for _ in 0..n { | |
let v = vec.next().unwrap().unwrap(); | |
let mut i = v.split_whitespace(); | |
let x = i.next().unwrap().parse::<i64>().unwrap(); | |
let y = i.next().unwrap().parse::<i64>().unwrap(); | |
writeln!(&mut ans, "{} {}", x, y).ok(); | |
} | |
println!("{}", ans); | |
} |
This file contains hidden or 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
//https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=9b0bc18d0a49960bbfc6149dfc708142 | |
// read multiple lines where the number of lines are not known upto <EOF> | |
//output the input as is | |
use std::fmt::Write; | |
use std::io::BufRead; | |
use std::io::{Cursor, BufReader}; | |
// for console input replace the above line with this | |
// use std::io::stdin; | |
fn main() { | |
// simulate stdin in rust playground | |
let f = Cursor::new("3\n40 3\n30 4\n20 5".to_string()); | |
let input = BufReader::new(f); | |
let input = input.lines(); | |
// for console input replace the above lines with these | |
// let input = stdin(); | |
// let mut vec = input.lock().lines(); | |
let mut buf = String::new(); | |
for line in input { | |
writeln!(&mut buf, "{}", line.unwrap()).unwrap(); | |
} | |
print!("{}", buf); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment