0

I have function that returns a Result and I am using the ? operation on it. What is the catch? Function:

pub fn open(filename: &str) -> Result<Box<TIFF>> {
    let tiff_reader = TIFFReader;
    tiff_reader.load(filename)
}

MRE:

use geotiff::TIFF;

fn main() {
    let res = TIFF::open("geotiff.tif")?;
    println!("Hello, world!");
}

Compiler:

the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::Try`)

cannot use the `?` operator in a function that returns `()`

help: the trait `std::ops::Try` is not implemented for `()`
note: required by `std::ops::Try::from_error`rustc(E0277)
Ömer Erden
  • 7,680
  • 5
  • 36
  • 45
dclipca
  • 1,739
  • 1
  • 16
  • 51
  • 3
    Not *on* a function, but *in* a function. You do not show where you are calling `TIFF::open` (your question does not contain an [MRE](https://stackoverflow.com/help/minimal-reproducible-example)). – justinas Feb 05 '21 at 11:55
  • @justinas is added the code that I am using. – dclipca Feb 05 '21 at 11:58
  • 1
    Thank you, I think I got the docs wrong. It's supposed to be used inside a function. Please post this as an answer. – dclipca Feb 05 '21 at 12:00
  • 2
    Does the following help @virtumonde https://doc.rust-lang.org/edition-guide/rust-2018/error-handling-and-panics/question-mark-in-main-and-tests.html – Jason Feb 05 '21 at 12:01
  • Thank you @Jason this is perfect. I got it the other way initially. – dclipca Feb 05 '21 at 12:05
  • @justinas Yes, I agree this is a duplicate. – dclipca Feb 05 '21 at 12:06

0 Answers0