Impl Into<U> for Result<T, E> where T: Into<U>, E: Into<U>

Hi guys, I have a question, I have Result<T, E> where T implements Into<Vec> and E implements Into<Vec>, I don't see that I can write code like

let r: Result<T, E> = ...
let v: Vec<MyType> = r.into()

is it good proposal to standard library? or maybe here some concerns that I am missing

thanks

Are there use-cases for this?

It seems rare to have compatible types for both ok and error values, and to use either of them as the final value. For providing values in case of an error there are methods like .unwrap_or.

The "long" way to write it isn't too bad: match r {Ok(o) => o.into(), Err(e) => e.into()}, so I don't think this addition would be solving a big or common problem.

1 Like

There isn't a dedicated transform for it (and probably shouldn't, as the Result container has meaning that Into::into would be discarding), but the way of writing it out really isn't that much more complicated:

res.map(Into::into).unwrap_or_else(Into::into)

(Disclaimer: may need a type hint via using U::from instead.)

A shorter version of that:

res.map_or_else(Into::into, Into::into)
1 Like

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