rust - How to print! an Option<Box<struct>>? -


i trying print option<box<mystruct>>, compile error when trying implement display option<box<mystruct>>.

use std::fmt;  fn main() {     let maybe_my_struct: option<box<mystruct>> = some(box::new(mystruct{foo:42}));     println!("{}", maybe_my_struct); }  struct mystruct {     foo: i32, }  impl fmt::display option<box<mystruct>> {     fn fmt(&self, formatter: &mut fmt::formatter) -> fmt::result {         match self {             some(mystruct) => write!(formatter, "{}", self.foo),             none => write!(formatter, "no struct"),         }     } } 

the error :

error: impl not reference types defined in crate;  traits defined in current crate can implemented arbitrary types [e0117] 

i have tried aliasing option type, , instead implementing display myoption<box<mystruct>>, gives same result. doing wrong?

as can see, can't implement trait didn't write type didn't write. part of what's known "coherence" , exists prevent weird things linking against library causing unrelated parts of program change behaviour.

aliasing option myoption doesn't work either because, say, it's alias. is, it's name same thing, it's not actual, different type.

now, if write wrapper around option so:

struct myoption<t>(option<t>); 

then myoption new, distinct type can implement trait for. of course, you'll want write methods wrap , unwrap actual option you're storing.

... rather irrelevant since could also derive debug struct , use that.

fn main() {     let maybe_my_struct: option<box<mystruct>> = some(box::new(mystruct{foo:42}));     println!("{:?}", some(maybe_my_struct)); }  #[derive(debug)] struct mystruct {     foo: i32, } 

or, if want custom display logic option<box<mystruct>> combination, can use marker value (this same approach used path in standard library, incidentally). so:

use std::fmt;  fn main() {     let maybe_my_struct: option<box<mystruct>> = some(box::new(mystruct{foo:42}));     println!("{:?}", maybe_my_struct);      // instead of displaying directly, display via custom marker.     println!("{}", maybe_my_struct.display());     println!("{}", none::<box<mystruct>>.display()); }  #[derive(debug)] struct mystruct {     foo: i32, }  // marker we'll use define our custom display impl. struct mmsdisplay<'a>(&'a option<box<mystruct>>);  // trait lets extend option<box<mystruct>> new method. trait custommmsdisplay {     fn display<'a>(&'a self) -> mmsdisplay<'a>; }  impl custommmsdisplay option<box<mystruct>> {     fn display<'a>(&'a self) -> mmsdisplay<'a> {         mmsdisplay(self)     } }  // , here's display logic. impl<'a> fmt::display mmsdisplay<'a> {     fn fmt(&self, formatter: &mut fmt::formatter) -> fmt::result {         match *self.0 {             some(ref ms) => write!(formatter, "{}", ms.foo),             none => write!(formatter, "no struct"),         }     } } 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -