我有以下代码:
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));
我收到此错误:
错误:借来的值不够长让 mut canvas = Canvas::new(100, 100, &renderer)
为什么返回的Canvas
对象寿命不够长?如果我将结果存储在中间let
,那么它可以工作;为什么?