crosspostato da: https://lemmy.world/post/1916287
Hi everybody, I’m new to Rust.
So, I have a struct
Panel
which contains a datawidget
which implements the traitWidget
I have to implement a function forPanel
that uses another function that requires a type that implementsWidget
.I tried
Box<T>
,Rc<T>
,Box<dyn Widget
,, but nothing, always compiler errors.
How can I fix this?
That function takes ownership of the Widget, there’s no borrowing here. Maybe that’s the problem, that you’re passing a reference instead of the actual object?
So, if you have a Panel containing the Widget, the function you’re calling
render_widget
in has to takeself
, not&self
or&mut self
.Alternatively, you can use
data: Option<T>
in Panel, let the function take&mut self
and then useself.data.take()
to move it out of self.I checked the code and noticed that I wrote
&self
as the parameter 🤦🏻Now everything works. Thanks man