Confusing over Vec and Dyn (NewB q)

Hello World,

i have some structs like Control .Control>Edit> enz
how do I put those in a vec!
Here is sample code that better explains what i am trying to do

pub trait ControlMarker {} 
pub struct Control<D: ControlMarker> {
    name: String,
    _isFocused: bool,
    _isVisible: bool,
    _isEnabled: bool,
    _Flags: u8,
    _control: D,
impl<T: ControlMarker> Control<T> {
    pub fn SetIsFocused(&mut self, new_focus: bool) {
        self._isFocused = new_focus
    pub fn SetIsVisible(&mut self, new_visible: bool) {
        self._isVisible = new_visible
    pub fn SetIsEnabled(&mut self, new_enabled: bool) {
        self._isEnabled = new_enabled
    pub fn IsFocused(&self) -> bool {
    pub fn IsVisible(&self) -> bool {
    pub fn IsEnabled(&self) -> bool {
pub struct Toggle {toggle: bool,}
impl ControlMarker for Toggle {}
impl Control<Toggle> {
    pub fn Flip(&mut self) {
        self._control.toggle = !(self._control.toggle)
struct Editable {buffer: String,}
impl ControlMarker for Editable {}
impl Control<Editable> {
    pub fn StartEdit(&self) -> () { /* Strat here */

impl ControlMarker for Box<(dyn ControlMarker + 'static)> {} // o o bad sign.....

struct Host {
    widgets: Vec<Control<Box<dyn ControlMarker>>>, // here starts the confusion....
impl Host {
    pub fn EnableAll(&mut self) {
        for w in &mut self.widgets {

pub fn main() {
    let mut ToggleButton = Control::<Toggle>::default();
    let mut Entry = Control::<Editable>::default();


    let cnt = Host {widgets: vec![(ToggleButton), (Entry)],}; // ofcourse this doent work :(

or platgound permalink



You’re having values of type Control<Toggle> and Control<Editable> and trying to put them into a Vec<Control<Box<dyn ControlMarker>>>. I’m not quite sure what the requirements in your code here are why you’re even needing trait objects. The code presented doesn’t have any methods in ControlMarker, so the use case for trait objects doesn’t entirely become clear.

If you want trait objects, there’s a few approaches to make your code example compile.


Tnx! Steffahn ,

The requirements are that i get a Vec with items like:
Control[entry], Control[button], Control[List] enz.
I thought because of the 'T' i should use Dyn to store them in a Vec.
Am I wrong about that?

Nevertheless your solutions work.

Tnx again and regards,


Right, maybe that wasn’t clear: my main point was that as long as ControlMarker doesn’t have any methods, you’re basically storing some inaccessible garbage data in your Box<dyn ControlMarker> (well, at least unless they’re supposed to do important stuff when dropped). Thus assuming that your code presented may be a simplification of your actual setting, I was anticipating that there are more things to the ControlMarker trait or more ways in which the Vec<… Control … ControlMarker …> is used. And then, some of the proposed solutions might become infeasible once the simplifications are no longer in place. Or if there isn’t much more to the situation than what you presented, you might as-well swap out the ControlMarker types with some fixed dummy type before putting them into the Vec, so you don’t need trait objects at all.

1 Like

:+1: :+1: :+1: :+1: :+1:
Learned some new things today


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.