From example question

The From trait has this example, which I'm not understanding. It looks like the only possible return type of the example function is Ok(i32). I'm not sure where CliError would ever happen since everything else is just unwrapped.

Wouldn't the program 100% of the time either panic or return an Ok(i32)? I thought the point was to implement From for the enum variants and then return one of those variants at some point.

Key point is ? operator. Basically let a = expr?; will be desugared to something like below.

let a = match expr {
  Ok(res) => res,
  Err(e) => return Err(e.into()),
};

Note that this pseudo-desugaring is not really accurate as you can ? on Option<T>, and in future just every type that impls Try trait(can be renamed).

Ah, I see! I was thinking ? was the same as unwrap(). Thanks!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.