I can read files in rust
This commit is contained in:
parent
09bf3f1e67
commit
8cabe40643
|
@ -1,17 +1,31 @@
|
||||||
use std::fs::File;
|
|
||||||
use std::env;
|
use std::env;
|
||||||
|
use std::fs::File;
|
||||||
use std::io::{self, BufRead};
|
use std::io::{self, BufRead};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
fn main() -> std::io::Result<()> {
|
|
||||||
let fileName = env::args().nth(1);
|
fn main() {
|
||||||
let fileLines = read_lines(fileName);
|
let args: Vec<String> = env::args().collect();
|
||||||
Ok(())
|
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>>>
|
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
||||||
where P: AsRef<Path>, {
|
where P: AsRef<Path>, {
|
||||||
let file = File::open(filename)?;
|
let file = File::open(filename)?;
|
||||||
Ok(io::BufReader::new(file).lines())
|
Ok(io::BufReader::new(file).lines())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue