Return &[&dyn Trait] from member Vec>

⚓ rust    📅 2025-05-16    👤 surdeus    👁️ 4      

surdeus

Warning

This post was published 44 days ago. The information described in this article may have changed.

I have a struct similar to this:

struct MyStruct {
    things: Vec<Box<dyn MyTrait>>
}

And I'd like to be able to do this:

fn get_all_things(&self) -> &[&dyn MyTrait] {
    how?
}

i.e. I'd like to return a slice of references to something that implements the trait rather than &[Box<dyn MyTrait>].

Is it possible?

7 posts - 5 participants

Read full topic

🏷️ rust_feed