我有以下代码:
pub struct Canvas<'a> {
width: isize,
height: isize,
color: Color,
surface: Surface,
texture: Texture,
renderer: &'a Renderer,
}
impl<'a> Canvas<'a> {
pub fn new(width: isize, height: isize, renderer: &'a Renderer) -> Canvas<'a> {
let color = Color::RGB(0, 30, 0);
let mut surface = core::create_surface(width, height);
let texture = Canvas::gen_texture(&mut surface, width, height, color, renderer);
Canvas {
width: width,
height: height,
color: color,
surface: surface,
texture: texture,
renderer: renderer,
}
}
pub fn color(&mut self, color: Color) -> &mut Canvas<'a> …Run Code Online (Sandbox Code Playgroud) tl;drerror[E0716]: temporary value dropped while borrowed是一个困难且常见的问题,有一致的解决方案吗?
我遇到了困难的 rustc 错误
error[E0716]: temporary value dropped while borrowed
...
creates a temporary which is freed while still in use
Run Code Online (Sandbox Code Playgroud)
搜索Stackoverflow,有很多关于这个rust error的问题error[E0716]。
也许 Rust 专家可以为这个常见的新手问题提供一个通用的解决方案,一个足够好的解决方案,它也可以回答链接的问题(见下文)。
一个简洁的代码示例来演示该问题(Rust Playground):
type Vec1<'a> = Vec::<&'a String>;
fn fun1(s1: &String, v1: &mut Vec1) {
v1.insert(0, &s1.clone());
}
fn main() {
let mut vec1 = Vec::new();
let str1 = String::new();
fun1(&str1, &mut vec1);
}
Run Code Online (Sandbox Code Playgroud)
结果:
error[E0716]: temporary value dropped while borrowed
--> …Run Code Online (Sandbox Code Playgroud)