I can read files in rust

This commit is contained in:
Nemo 2020-05-30 03:51:13 +05:30
parent 09bf3f1e67
commit 8cabe40643
1 changed files with 20 additions and 6 deletions

View File

@ -1,17 +1,31 @@
use std::fs::File;
use std::env;
use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
fn main() -> std::io::Result<()> {
let fileName = env::args().nth(1);
let fileLines = read_lines(fileName);
Ok(())
fn main() {
let args: Vec<String> = env::args().collect();
let filename = parse_config(&args);
if let Ok(lines) = read_lines(Path::new(filename)) {
// Consumes the iterator, returns an (Optional) String
for line in lines {
if let Ok(ip) = line {
println!("{}", ip);
}
}
}
}
fn parse_config(args: &[String]) -> &str {
let filename = &args[1];
filename
}
// The output is wrapped in a Result to allow matching on errors
// Returns an Iterator to the Reader of the lines of the file.
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where P: AsRef<Path>, {
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}