Rust idiomatic way of polymorphic struct

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












I've implemented a simple polymorphic structure, which looks a bit ugly and I'm afraid I am missing an idiomatic way of doing it.



To describe briefly what I'm trying to achieve here: I would like to have a structure with a collection of handlers, which all must implement specific trait.



use std::fmt;
use std::fmt::Formatter;
use std::fmt::Error;

trait Handler
fn get_type(&self) -> String;


struct FileHandler


struct DbHandler


impl Handler for FileHandler
fn get_type(&self) -> String
"file".to_string()



impl Handler for DbHandler
fn get_type(&self) -> String
"db".to_string()



struct Config
handlers: Vec<Box<Handler>>


impl fmt::Debug for Config
fn fmt(&self, f: &mut Formatter) -> Result<(), Error>
for h in self.handlers.iter()
println!("", h.get_type());

write!(f, "config")



pub fn run()
let mut handlers: Vec<Box<Handler>> = Vec::new();

let fh = FileHandler ;
let dh = DbHandler ;

handlers.push(Box::new(fh));
handlers.push(Box::new(dh));

let c = Config handlers ;
println!(":?", c);



Is there a way of more cleaner or more idiomatic solution here?







share|improve this question

















  • 1




    Can you expand on what you mean by "looks a bit ugly"? Ugliness is not a universally shared concept.
    – Shepmaster
    Apr 17 at 17:04






  • 1




    @Shepmaster It's ugly for me as a newcomer to rust, practically, not in a way that rust is ugly, but how I've done it =) But I can't really say how it should look in a better implementation. I guess by "ugly" I mean I'm not quite sure it's idiomatic enough.
    – Stormherz
    Apr 17 at 17:09







  • 1




    Just a little question: why is Config generic?
    – Boiethios
    Apr 18 at 7:40






  • 1




    Not a full answer to the question, but a couple of stylistic tweaks you might like - you can replace the self.handlers.iter() in your for loop with &self.handlers (as &Vec<T> implements IntoIterator<&T>) , and you can drop the braces from structs with no fields.
    – Joe Clay
    Apr 18 at 15:38






  • 1




    You probably meant to use writeln!(f, ...)?; in your Debug impl instead of println!(...);.
    – Francis Gagné
    Apr 19 at 2:08
















up vote
1
down vote

favorite












I've implemented a simple polymorphic structure, which looks a bit ugly and I'm afraid I am missing an idiomatic way of doing it.



To describe briefly what I'm trying to achieve here: I would like to have a structure with a collection of handlers, which all must implement specific trait.



use std::fmt;
use std::fmt::Formatter;
use std::fmt::Error;

trait Handler
fn get_type(&self) -> String;


struct FileHandler


struct DbHandler


impl Handler for FileHandler
fn get_type(&self) -> String
"file".to_string()



impl Handler for DbHandler
fn get_type(&self) -> String
"db".to_string()



struct Config
handlers: Vec<Box<Handler>>


impl fmt::Debug for Config
fn fmt(&self, f: &mut Formatter) -> Result<(), Error>
for h in self.handlers.iter()
println!("", h.get_type());

write!(f, "config")



pub fn run()
let mut handlers: Vec<Box<Handler>> = Vec::new();

let fh = FileHandler ;
let dh = DbHandler ;

handlers.push(Box::new(fh));
handlers.push(Box::new(dh));

let c = Config handlers ;
println!(":?", c);



Is there a way of more cleaner or more idiomatic solution here?







share|improve this question

















  • 1




    Can you expand on what you mean by "looks a bit ugly"? Ugliness is not a universally shared concept.
    – Shepmaster
    Apr 17 at 17:04






  • 1




    @Shepmaster It's ugly for me as a newcomer to rust, practically, not in a way that rust is ugly, but how I've done it =) But I can't really say how it should look in a better implementation. I guess by "ugly" I mean I'm not quite sure it's idiomatic enough.
    – Stormherz
    Apr 17 at 17:09







  • 1




    Just a little question: why is Config generic?
    – Boiethios
    Apr 18 at 7:40






  • 1




    Not a full answer to the question, but a couple of stylistic tweaks you might like - you can replace the self.handlers.iter() in your for loop with &self.handlers (as &Vec<T> implements IntoIterator<&T>) , and you can drop the braces from structs with no fields.
    – Joe Clay
    Apr 18 at 15:38






  • 1




    You probably meant to use writeln!(f, ...)?; in your Debug impl instead of println!(...);.
    – Francis Gagné
    Apr 19 at 2:08












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I've implemented a simple polymorphic structure, which looks a bit ugly and I'm afraid I am missing an idiomatic way of doing it.



To describe briefly what I'm trying to achieve here: I would like to have a structure with a collection of handlers, which all must implement specific trait.



use std::fmt;
use std::fmt::Formatter;
use std::fmt::Error;

trait Handler
fn get_type(&self) -> String;


struct FileHandler


struct DbHandler


impl Handler for FileHandler
fn get_type(&self) -> String
"file".to_string()



impl Handler for DbHandler
fn get_type(&self) -> String
"db".to_string()



struct Config
handlers: Vec<Box<Handler>>


impl fmt::Debug for Config
fn fmt(&self, f: &mut Formatter) -> Result<(), Error>
for h in self.handlers.iter()
println!("", h.get_type());

write!(f, "config")



pub fn run()
let mut handlers: Vec<Box<Handler>> = Vec::new();

let fh = FileHandler ;
let dh = DbHandler ;

handlers.push(Box::new(fh));
handlers.push(Box::new(dh));

let c = Config handlers ;
println!(":?", c);



Is there a way of more cleaner or more idiomatic solution here?







share|improve this question













I've implemented a simple polymorphic structure, which looks a bit ugly and I'm afraid I am missing an idiomatic way of doing it.



To describe briefly what I'm trying to achieve here: I would like to have a structure with a collection of handlers, which all must implement specific trait.



use std::fmt;
use std::fmt::Formatter;
use std::fmt::Error;

trait Handler
fn get_type(&self) -> String;


struct FileHandler


struct DbHandler


impl Handler for FileHandler
fn get_type(&self) -> String
"file".to_string()



impl Handler for DbHandler
fn get_type(&self) -> String
"db".to_string()



struct Config
handlers: Vec<Box<Handler>>


impl fmt::Debug for Config
fn fmt(&self, f: &mut Formatter) -> Result<(), Error>
for h in self.handlers.iter()
println!("", h.get_type());

write!(f, "config")



pub fn run()
let mut handlers: Vec<Box<Handler>> = Vec::new();

let fh = FileHandler ;
let dh = DbHandler ;

handlers.push(Box::new(fh));
handlers.push(Box::new(dh));

let c = Config handlers ;
println!(":?", c);



Is there a way of more cleaner or more idiomatic solution here?









share|improve this question












share|improve this question




share|improve this question








edited Apr 18 at 8:06
























asked Apr 17 at 17:02









Stormherz

1062




1062







  • 1




    Can you expand on what you mean by "looks a bit ugly"? Ugliness is not a universally shared concept.
    – Shepmaster
    Apr 17 at 17:04






  • 1




    @Shepmaster It's ugly for me as a newcomer to rust, practically, not in a way that rust is ugly, but how I've done it =) But I can't really say how it should look in a better implementation. I guess by "ugly" I mean I'm not quite sure it's idiomatic enough.
    – Stormherz
    Apr 17 at 17:09







  • 1




    Just a little question: why is Config generic?
    – Boiethios
    Apr 18 at 7:40






  • 1




    Not a full answer to the question, but a couple of stylistic tweaks you might like - you can replace the self.handlers.iter() in your for loop with &self.handlers (as &Vec<T> implements IntoIterator<&T>) , and you can drop the braces from structs with no fields.
    – Joe Clay
    Apr 18 at 15:38






  • 1




    You probably meant to use writeln!(f, ...)?; in your Debug impl instead of println!(...);.
    – Francis Gagné
    Apr 19 at 2:08












  • 1




    Can you expand on what you mean by "looks a bit ugly"? Ugliness is not a universally shared concept.
    – Shepmaster
    Apr 17 at 17:04






  • 1




    @Shepmaster It's ugly for me as a newcomer to rust, practically, not in a way that rust is ugly, but how I've done it =) But I can't really say how it should look in a better implementation. I guess by "ugly" I mean I'm not quite sure it's idiomatic enough.
    – Stormherz
    Apr 17 at 17:09







  • 1




    Just a little question: why is Config generic?
    – Boiethios
    Apr 18 at 7:40






  • 1




    Not a full answer to the question, but a couple of stylistic tweaks you might like - you can replace the self.handlers.iter() in your for loop with &self.handlers (as &Vec<T> implements IntoIterator<&T>) , and you can drop the braces from structs with no fields.
    – Joe Clay
    Apr 18 at 15:38






  • 1




    You probably meant to use writeln!(f, ...)?; in your Debug impl instead of println!(...);.
    – Francis Gagné
    Apr 19 at 2:08







1




1




Can you expand on what you mean by "looks a bit ugly"? Ugliness is not a universally shared concept.
– Shepmaster
Apr 17 at 17:04




Can you expand on what you mean by "looks a bit ugly"? Ugliness is not a universally shared concept.
– Shepmaster
Apr 17 at 17:04




1




1




@Shepmaster It's ugly for me as a newcomer to rust, practically, not in a way that rust is ugly, but how I've done it =) But I can't really say how it should look in a better implementation. I guess by "ugly" I mean I'm not quite sure it's idiomatic enough.
– Stormherz
Apr 17 at 17:09





@Shepmaster It's ugly for me as a newcomer to rust, practically, not in a way that rust is ugly, but how I've done it =) But I can't really say how it should look in a better implementation. I guess by "ugly" I mean I'm not quite sure it's idiomatic enough.
– Stormherz
Apr 17 at 17:09





1




1




Just a little question: why is Config generic?
– Boiethios
Apr 18 at 7:40




Just a little question: why is Config generic?
– Boiethios
Apr 18 at 7:40




1




1




Not a full answer to the question, but a couple of stylistic tweaks you might like - you can replace the self.handlers.iter() in your for loop with &self.handlers (as &Vec<T> implements IntoIterator<&T>) , and you can drop the braces from structs with no fields.
– Joe Clay
Apr 18 at 15:38




Not a full answer to the question, but a couple of stylistic tweaks you might like - you can replace the self.handlers.iter() in your for loop with &self.handlers (as &Vec<T> implements IntoIterator<&T>) , and you can drop the braces from structs with no fields.
– Joe Clay
Apr 18 at 15:38




1




1




You probably meant to use writeln!(f, ...)?; in your Debug impl instead of println!(...);.
– Francis Gagné
Apr 19 at 2:08




You probably meant to use writeln!(f, ...)?; in your Debug impl instead of println!(...);.
– Francis Gagné
Apr 19 at 2:08















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f192308%2frust-idiomatic-way-of-polymorphic-struct%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f192308%2frust-idiomatic-way-of-polymorphic-struct%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Chat program with C++ and SFML

Function to Return a JSON Like Objects Using VBA Collections and Arrays

Will my employers contract hold up in court?