Hello great community. i have a confussion, this struct have a method find to return a option:
how parse year to i32 ??
struct TextFile {
lines: Vec<String>,
}
impl TextFile {
fn new(filename: &str) -> io::Result<TextFile> {
let reader = fs::read_to_string(filename)
.expect("Except: file info.txt not exist");
let liness = reader.lines();
let lines: Vec<String> = liness.map(String::from).collect(); // can't iterate &str to gain a vec of String
Ok(TextFile {lines})
}
fn _print_lines(&self) {
for line in &self.lines {
println!("{}", line);
}
}
fn find(&self, key:&str) -> Option<&str> {
let mut found: bool = false;
for line in &self.lines {
// ... you know you return the line AFTER the line where you found `key`, right?
if found {
return Some(line);
} else if line.contains(key) {
found = true;
}
}
None
}
}
// var to store values
let mut original_title: Option<&str> = None;
let mut year: Option<&str> = None; // how convert to i32 or store i32? :woozy_face:
let mut country: Option<&str> = None;
let text_file = TextFile::new(/home/pichi/info.txt);
match &text_file {
Ok(file) => { //file.print_lines();
original_title = Some(file.find("Original title").unwrap().trim());
year = Some(file.find("Year").unwrap().trim());
country = Some(file.find("Country").unwrap().trim()); },
Err(err) => println!("error {}", err),
}
LAST ERROR:
year: year.expect("REASON").parse::(),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected Option<i32>
, found Result<i32, ParseIntError>