use std::convert::{ TryInto, TryFrom, }; type BoxError = Box; type XResult = Result; fn do_print(t: T) -> XResult<()> where T: TryInto, T::Error: Into { println!("--> {}", t.try_into().map_err(Into::into)?); Ok(()) } fn do_print2(t: T) where T: Into { println!("==> {}", t.into()) } struct StructX { name: String, } struct StructY { name: String, } // auto have: TryInto for StructX impl TryFrom for String { type Error = BoxError; fn try_from(sx: StructX) -> XResult { Ok(format!("NAME: {}", sx.name)) } } impl From for String { fn from(sx: StructY) -> Self { format!("NAME:: {}", sx.name) } } fn main() -> XResult<()> { let x = StructX { name: "hater".to_owned(), }; do_print(x)?; let y = StructY { name: "hater".to_owned(), }; do_print2(y); Ok(()) }