如何为不是函数参数的引用设置生命周期?
How can I set the lifetime for a reference which isn't an argument of the function?
我正在尝试编译这段代码。函数 get_lines
必须生成一个 Line
的向量,将新行推送到它并 return 这个向量。
fn main() {
let begin_point = Point{x:100, y:100};
let end_point = Point{x:300, y:100};
let lines = get_lines( &begin_point, &end_point);
}
fn get_lines<'a>(begin_point: &'a Point, end_point: &'a Point) -> Vec<Line<'a>>
{
let mut lines: Vec<Line> = vec![];
let middle_color = Color{r:0,g:0,b:0};
let line = Line {
begin_point: begin_point,
end_point: end_point,
color: &middle_color,
};
lines.push(line);
lines
}
struct Color<> {r:i32, g:i32, b:i32}
struct Point {x:i32, y:i32}
struct Line<'a> {
begin_point: &'a Point,
end_point: &'a Point,
color: &'a Color
}
编译失败并出现此错误:
src\main.rs:16:17: 16:29 error: `middle_color` does not live long enough
src\main.rs:16 color: &middle_color,
^~~~~~~~~~~~
src\main.rs:9:1: 21:2 note: reference must be valid for the lifetime 'a as defined on the block at 9:0...
src\main.rs: 9 {
src\main.rs:10 let mut lines: Vec<Line> = vec![];
src\main.rs:11 let middle_color = Color{r:0,g:0,b:0};
src\main.rs:12
src\main.rs:13 let line = Line {
src\main.rs:14 begin_point: begin_point,
...
src\main.rs:11:43: 21:2 note: ...but borrowed value is only valid for the block suffix following statement 1 at 11:42
src\main.rs:11 let middle_color = Color{r:0,g:0,b:0};
src\main.rs:12
src\main.rs:13 let line = Line {
src\main.rs:14 begin_point: begin_point,
src\main.rs:15 end_point: end_point,
src\main.rs:16 color: &middle_color,
...
error: aborting due to previous error
如何为不是函数参数的引用设置生命周期?还是代码逻辑有问题?
您收到错误,因为当 get_lines()
returns 时,middle_color
超出范围并被释放。如果不发生这种情况,您将在向量中得到一个悬空指针。
如果你真的想在Line
中使用对color
的引用,你可以在main
函数中实例化它并将对它的引用传递给get_lines()
,例如:
fn get_lines<'a>(begin_point: &'a Point, end_point: &'a Point, middle_color: &'a Color) -> Vec<Line<'a>>
{
...
}
但如果这样做,您甚至可以在 main
中创建 Line
并将其移至 get_lines()
。
我正在尝试编译这段代码。函数 get_lines
必须生成一个 Line
的向量,将新行推送到它并 return 这个向量。
fn main() {
let begin_point = Point{x:100, y:100};
let end_point = Point{x:300, y:100};
let lines = get_lines( &begin_point, &end_point);
}
fn get_lines<'a>(begin_point: &'a Point, end_point: &'a Point) -> Vec<Line<'a>>
{
let mut lines: Vec<Line> = vec![];
let middle_color = Color{r:0,g:0,b:0};
let line = Line {
begin_point: begin_point,
end_point: end_point,
color: &middle_color,
};
lines.push(line);
lines
}
struct Color<> {r:i32, g:i32, b:i32}
struct Point {x:i32, y:i32}
struct Line<'a> {
begin_point: &'a Point,
end_point: &'a Point,
color: &'a Color
}
编译失败并出现此错误:
src\main.rs:16:17: 16:29 error: `middle_color` does not live long enough
src\main.rs:16 color: &middle_color,
^~~~~~~~~~~~
src\main.rs:9:1: 21:2 note: reference must be valid for the lifetime 'a as defined on the block at 9:0...
src\main.rs: 9 {
src\main.rs:10 let mut lines: Vec<Line> = vec![];
src\main.rs:11 let middle_color = Color{r:0,g:0,b:0};
src\main.rs:12
src\main.rs:13 let line = Line {
src\main.rs:14 begin_point: begin_point,
...
src\main.rs:11:43: 21:2 note: ...but borrowed value is only valid for the block suffix following statement 1 at 11:42
src\main.rs:11 let middle_color = Color{r:0,g:0,b:0};
src\main.rs:12
src\main.rs:13 let line = Line {
src\main.rs:14 begin_point: begin_point,
src\main.rs:15 end_point: end_point,
src\main.rs:16 color: &middle_color,
...
error: aborting due to previous error
如何为不是函数参数的引用设置生命周期?还是代码逻辑有问题?
您收到错误,因为当 get_lines()
returns 时,middle_color
超出范围并被释放。如果不发生这种情况,您将在向量中得到一个悬空指针。
如果你真的想在Line
中使用对color
的引用,你可以在main
函数中实例化它并将对它的引用传递给get_lines()
,例如:
fn get_lines<'a>(begin_point: &'a Point, end_point: &'a Point, middle_color: &'a Color) -> Vec<Line<'a>>
{
...
}
但如果这样做,您甚至可以在 main
中创建 Line
并将其移至 get_lines()
。