RosettaCodeData/Task/Read-a-specific-line-from-a-file/Rust/read-a-specific-line-from-a-file-1.rust
2018-06-22 20:57:24 +00:00

19 lines
566 B
Text

use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
use std::io::Error;
use std::path::Path;
fn main() {
let path = Path::new("file.txt");
let line_num = 7usize;
let line = get_line_at(&path, line_num - 1);
println!("{}", line.unwrap());
}
fn get_line_at(path: &Path, line_num: usize) -> Result<String, Error> {
let file = File::open(path).expect("File not found or cannot be opened");
let content = BufReader::new(&file);
let mut lines = content.lines();
lines.nth(line_num).expect("No line found at that position")
}