"borrowed value does not live long enough" 使用构建器模式时
"borrowed value does not live long enough" when using the builder pattern
我有以下代码:
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> {
self.color = color;
self.texture = Canvas::gen_texture(
&mut self.surface,
self.width,
self.height,
self.color,
self.renderer,
);
self
}
}
我希望能够做到这一点:
let mut canvas = Canvas::new(100, 100, &renderer).color(Color::RGB(80, 230, 80));
我收到这个错误:
error: borrowed value does not live long enough
let mut canvas = Canvas::new(100, 100, &renderer)
为什么返回的 Canvas
对象寿命不够长?如果我将结果存储在中间 let
中,那么它就可以工作;为什么?
这是一个最小的复制:
#[derive(Debug)]
pub struct Canvas;
impl Canvas {
fn new() -> Self {
Canvas
}
fn color(&self) -> &Canvas {
self
}
}
fn main() {
let mut canvas = Canvas::new().color();
// 1 ^~~~~~~~~~~~~
// 2 ^~~~~
println!("{:?}", canvas);
}
生锈 2015
error[E0597]: borrowed value does not live long enough
--> src/main.rs:15:22
|
15 | let mut canvas = Canvas::new().color();
| ^^^^^^^^^^^^^ - temporary value dropped here while still borrowed
| |
| temporary value does not live long enough
...
19 | }
| - temporary value needs to live until here
|
= note: consider using a `let` binding to increase its lifetime
生锈 2018
error[E0716]: temporary value dropped while borrowed
--> src/main.rs:15:22
|
15 | let mut canvas = Canvas::new().color();
| ^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
| |
| creates a temporary which is freed while still in use
...
18 | println!("{:?}", canvas);
| ------ borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
问题的出现是因为您创建了一个临时变量 (1),然后将对该变量的引用传递给方法 (2),该方法 return 是引用。在方法链的末尾,您试图 return 引用并将其存储在变量中,但该引用指向一个 无处可去 的临时项! Rust 不允许您引用无效的内容。
部分问题是这个不是构建器模式,这只是一个使用链接方法调用修改自身的结构.一些解决方案:
- 存储"temporary"变量,在这种情况下,所有方法都是之后发生的正常变异方法。
- 接受
self
而不是对自身的引用(&self
、&mut self
),然后最终 return 完整结构。
- 在链的末尾有一个
build
方法,return 是另一个独立的结构,而不是引用。
我有以下代码:
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> {
self.color = color;
self.texture = Canvas::gen_texture(
&mut self.surface,
self.width,
self.height,
self.color,
self.renderer,
);
self
}
}
我希望能够做到这一点:
let mut canvas = Canvas::new(100, 100, &renderer).color(Color::RGB(80, 230, 80));
我收到这个错误:
error: borrowed value does not live long enough let mut canvas = Canvas::new(100, 100, &renderer)
为什么返回的 Canvas
对象寿命不够长?如果我将结果存储在中间 let
中,那么它就可以工作;为什么?
这是一个最小的复制:
#[derive(Debug)]
pub struct Canvas;
impl Canvas {
fn new() -> Self {
Canvas
}
fn color(&self) -> &Canvas {
self
}
}
fn main() {
let mut canvas = Canvas::new().color();
// 1 ^~~~~~~~~~~~~
// 2 ^~~~~
println!("{:?}", canvas);
}
生锈 2015
error[E0597]: borrowed value does not live long enough
--> src/main.rs:15:22
|
15 | let mut canvas = Canvas::new().color();
| ^^^^^^^^^^^^^ - temporary value dropped here while still borrowed
| |
| temporary value does not live long enough
...
19 | }
| - temporary value needs to live until here
|
= note: consider using a `let` binding to increase its lifetime
生锈 2018
error[E0716]: temporary value dropped while borrowed
--> src/main.rs:15:22
|
15 | let mut canvas = Canvas::new().color();
| ^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
| |
| creates a temporary which is freed while still in use
...
18 | println!("{:?}", canvas);
| ------ borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
问题的出现是因为您创建了一个临时变量 (1),然后将对该变量的引用传递给方法 (2),该方法 return 是引用。在方法链的末尾,您试图 return 引用并将其存储在变量中,但该引用指向一个 无处可去 的临时项! Rust 不允许您引用无效的内容。
部分问题是这个不是构建器模式,这只是一个使用链接方法调用修改自身的结构.一些解决方案:
- 存储"temporary"变量,在这种情况下,所有方法都是之后发生的正常变异方法。
- 接受
self
而不是对自身的引用(&self
、&mut self
),然后最终 return 完整结构。 - 在链的末尾有一个
build
方法,return 是另一个独立的结构,而不是引用。