Timmmm
Timmmm

Reputation: 96547

How does the question mark operator convert error types?

This doesn't compile:

fn foo() -> std::io::Result<()> {
    todo!()
}

pub fn bar() -> anyhow::Result<()> {
    foo()
}

Nor does this:

pub fn bar() -> anyhow::Result<()> {
    foo().into()
}

But this does:

pub fn bar() -> anyhow::Result<()> {
    Ok(foo()?)
}

What is the mechanism behind the conversion that ? does, and is there any better way to do this conversion than Ok(...?)?

Upvotes: 1

Views: 2300

Answers (1)

Angelicos Phosphoros
Angelicos Phosphoros

Reputation: 3057

It is something like:

// r?
match r {
   Ok(v)=>v,
   Err(x)=>return Err(std::convert::Into::into(x)),
}

So your last code is something like this:

pub fn bar() -> anyhow::Result<()> {
  Ok(match foo(){
     Ok(x)=>x,
     Err(x)=>return Err(std::convert::Into::into(x)),
  })
}

Or this:

pub fn bar() -> anyhow::Result<()> {
  let r: () = match foo(){
     Ok(x)=>x,
     Err(x)=>return Err(std::convert::Into::into(x)),
  };
  Ok(r)
}

UPD

As @Masklinn said, it uses From trait instead Into and Try trait too. You can learn more here. It is quite surprising because documentation for trait Into says:

Prefer using Into over From when specifying trait bounds on a generic function to ensure that types that only implement Into can be used as well.

Finally, result code would look like this:

pub fn bar() -> anyhow::Result<()> {
  let r: () = match Try::into_result(foo()){
     Ok(x)=>x,
     Err(e)=>return Try::from_error(From::from(e)),
  };
  Ok(r)
}

Upvotes: 2

Related Questions