Is it necessary to convert Enum to String in this case?

I'm trying to print out regex matches but this error is trigged:

--> src/main.rs:14:31 | 14 | let capture = re.find(&line); | ^^^^^ expectedstr, found enum std::result::Result | = note: expected reference&str found reference&std::result::Result<String, std::io::Error>

extern crate regex;
use regex::Regex;

use std::fs::File;
use std::io::{BufReader, Result, BufRead};

fn main() -> Result<()> { 
    let file = File::open("logs.log")?;
    let reader = BufReader::new(file);
    
    let re = Regex::new(r"^\w{3}\s{1,2}\d{1,2}\s(?:\d{2}:){2}\d{2}").unwrap();

    for line in reader.lines(){
        let capture = re.find(&line);
        println!("{:?}",capture);
    }

    Ok(())
}

The type of line is Result<String, io::Error> because reading the next line may result in an IO error. You need to handle that error somehow, e.g. by adding

let line = line?;

at the start of the loop.

1 Like

Clarify me please,

Result<()> = io::Result = Result<T, E>

That is the same thing?

io::Result<T> is an alias for Result<T, io::Error>.

This means that io::Result<()> is a short way to write Result<(), io::Error>.

4 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.