Confusion with "Result" type


#1

for reading a file line by line in rust , i got a ready made function that has result return type and whenever i try to call it in my code , it just shoots an error

here is the code

use std::fs::File;
use std::io::{BufRead, BufReader, Error, ErrorKind, Read};

fn main() {
    let  fname = "ix.txt";
    match rf(*fname) 
    {
    Ok(x) => println!("file open sucess ");
    Err(x) => println!("file openinin failure");
    };


}

fn rf<R: Read>(io: R) -> Result<Vec<i64>, Error> {
    let br = BufReader::new(io);
    let mut v = vec![];
    for line in br.lines() {
        v.push(line?
            .trim()
            .parse()
            .map_err(|e| Error::new(ErrorKind::InvalidData, e))?);
    }
    Ok(v)
}

here is the error

C:\Users\Dell\Desktop\rust\main\t>cargo build --release
   Compiling t v0.1.0 (file:///C:/Users/Dell/Desktop/rust/main/t)
error: expected one of `,`, `.`, `?`, `}`, or an operator, found `;`
  --> src\main.rs:17:48
   |
17 |     Ok(x) => println!("file open sucess {x}",x);
   |           --                                   ^ expected one of `,`, `.`, `?`, `}`, or an operator here
   |           |
   |           while parsing the `match` arm starting here

warning: unused import: `std::fs::File`
 --> src\main.rs:1:5
  |
1 | use std::fs::File;
  |     ^^^^^^^^^^^^^
  |
  = note: #[warn(unused_imports)] on by default

error[E0277]: the trait bound `str: std::marker::Sized` is not satisfied
  --> src\main.rs:15:11
   |
15 |     match rf(*fname)
   |           ^^ `str` does not have a constant size known at compile-time
   |
   = help: the trait `std::marker::Sized` is not implemented for `str`
note: required by `rf`
  --> src\main.rs:24:1
   |
24 | fn rf<R: Read>(io: R) -> Result<Vec<i64>, Error> {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0277]: the trait bound `str: std::io::Read` is not satisfied
  --> src\main.rs:15:11
   |
15 |     match rf(*fname)
   |           ^^ the trait `std::io::Read` is not implemented for `str`
   |
note: required by `rf`
  --> src\main.rs:24:1
   |
24 | fn rf<R: Read>(io: R) -> Result<Vec<i64>, Error> {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0277`.
error: Could not compile `t`.

#2

As the error mentions, sorta, you need to change the two arms’ ; to ,


#3

Hi, there are some errors in your code …

First as @steveklabnik you need to change ; to , for single line match arms, otherwise wrap match arms in { }.
Then what I guess you want to do is to receive an fname of type &str in rf method and remove the generics there and do File::open(fname) inside of rf,
And last do not dereference &str just use it as a reference.