I'm trying to read a file that is formatted like this:
ruby 2.6.2
elixir 1.8.3
And convert into a two-dimensional array like this pseudocode:
[
["ruby", "2.6.2"]
["elixir", "1.8.3"]
]
The code I have to do this in Rust is:
use std::fs::File;
use std::io::prelude::*;
use std::io::{self, BufReader};
use std::path::Path;
pub fn present() -> bool {
path().exists()
}
pub fn parse() -> Vec<Vec<String>> {
let f = BufReader::new(file().unwrap());
f.lines()
.map(|line| {
line.unwrap()
.split_whitespace()
.map(|x| x.to_string())
.collect()
})
.collect()
}
fn file() -> io::Result<File> {
let f = File::open(path())?;
return Ok(f);
}
fn path<'a>() -> &'a Path {
return Path::new(".tool-versions");
}
What I'm unsure of here, is this line in the middle of the parse
function:
.map(|x| x.to_string())
This seems like a bit of "overwork", but I am not sure if my feeling is correct here.
Is there something I'm missing here, or is this the cleanest way to write this code to accomplish this particular task?