我是生锈新手,我试图返回一个Window结构:
pub struct Window<'a> {
ev_loop: EventLoop<()>,
window_builder: WindowBuilder,
context_builder: ContextBuilder<'a, NotCurrent>,
display: Display
}从这一职能:
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
}
}但是,当以window_builder的形式返回wb,以context_builder的形式返回cb时,我会得到以下错误:
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我使用的是glium,它是一个库,所以我不能只修改库代码。我怎么才能解决这个问题?
发布于 2022-10-23 10:27:50
解决这一问题的办法是使用参考&。
但是由于glium::Display::new()不接受引用(请参阅docs),您需要克隆上下文构建器(又名cb )。
看,ContextBuilder在source中做#[derive(Clone)],所以您可以轻松地在代码中使用.clone()。
对不起,但是如果您不想move对象(也称为失去对象),您可以通过引用传递或克隆对象,但这是如何在生锈时这样做的。
这个答案适用于您确实希望将上下文生成器(cb)保存在Window结构中的情况。
另一个大胆的解决方案(来自@Jmb的建议:"Do you really need to keep the Window Builder and the Context Builder after you've used them to build the Display?")是从Window中删除cb字段。
祝你的项目好运!
发布于 2022-10-23 11:51:08
回答您的问题:您可以使用core::ptr::read取消指向非复制值的引用指针。请注意,这是不安全的,并可能导致双重自由。
这真的不是最好的方法,最好看看阿列克桑德说的话。
https://stackoverflow.com/questions/74170095
复制相似问题