rust - Option<T> where T can be two different traits? -


if have 2 different traits:

trait foo {} trait bar {} 

is possible have option can either of them (or none, of course), like:

struct foobar {   fb: option<~foo or bar> }  let fb1 = foobar{fb: some(~somestruct ~foo)} let fb2 = foobar{fb: some(~otherstruct ~bar)} 

and have them both work?

there nothing special option<t>; this, plus convenience methods , bit of documentation:

pub enum option<t> {     some(t),     none, } 

bear in mind: it's enums way. can make own enums. explicit, learn appreciate thing.

pub enum fooorbar {     foo(~foo),     bar(~bar), } 

you can make option<t> of this. or perhaps prefer blend two, if can better semantic meaning out of it:

pub enum {     foozy(~foo),     bark(~bar),     adifferentvariant, } 

Comments

Popular posts from this blog

c++ - How to add Crypto++ library to Qt project -

jQuery Mobile app not scrolling in Firefox -

How to use vim as editor in Matlab GUI -