I'm new to rust and I'm trying to return a Window
struct:
pub struct Window<'a> {
ev_loop: EventLoop<()>,
window_builder: WindowBuilder,
context_builder: ContextBuilder<'a, NotCurrent>,
display: Display
}
from this function:
pub fn init_lib () -> Window<'static> {
let event_loop = glutin::event_loop::EventLoop::new();
let wb = glutin::window::WindowBuilder::new();
let cb = glutin::ContextBuilder::new();
let display = glium::Display::new(wb, cb, &event_loop).unwrap();
return Window {
ev_loop: event_loop,
window_builder: wb,
context_builder: cb,
display: display
}
}
however whilst returning wb
as window_builder
and cb
as context_builder
I get the following error:
use of moved value: `cb`
value used here after moverustcE0382
lib.rs(32, 43): value moved here
lib.rs(31, 9): move occurs because `cb` has type `ContextBuilder<'_, NotCurrent>`, which does not implement the `Copy` trait
I'm using glium
which is a library, so I can't just modify the library code. How can I fix this?
the solution to this siutation was to use reference &
.
but since the glium::Display::new()
doesnt accept references (see the docs
), you need to clone the context builder aka cb
.
look, ContextBuilder
does #[derive(Clone)]
in the source
, so you can easily .clone()
in your code.
sorry, but is how you can do things like that in rust, either you pass by reference or you clone it, if you dont want to move
the object (aka lose the object)
this answer is for the case where you really want to keep context builder (cb
) inside your Window
struct
another bold solution (from what @Jmb suggested: "Do you really need to keep the Window Builder and the Context Builder after you've used them to build the Display?"
) is to remove the cb
field from Window
.
good luck to your project!