I am trying to use Rustlang on a Online Judge System, and it should get input from commond lines, my solution is as follow, and I am looking for better way to get input from commond lines:
use std::io::Read;
fn get_input(){
let mut buffer=String::new();
std::io::stdin().read_to_string(&mut buffer).unwrap();
let mut ans:Vec<Vec<i32>>=Vec::new();
for li in buffer.lines(){
let nums= li.trim().split(' ').map(|e| e.parse::<i32>().unwrap()).collect();
ans.push(nums);
}
println!("{:?}",ans);
}
fn main() {
get_input();
}
The input will looks like this:
5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1
And I want to the output looks like:
[[5, 6, 0, 2],
[1, 2, 1, 5, 3],
[0, 1, 1],
[0, 2, 2],
[0, 3, 1],
[1, 2, 1],
[2, 4, 1],
[3, 4, 1]]
A sample exame question is as follows: