Hello there, I have a simple struct which looks like this:
pub struct Graph<'a> {
verts: Vec<Vertex>,
edges: Vec<(&'a Vertex, &'a Vertex)>,
}
What I want is not exacly clear here: I want the tuple of Vertex References in Graph::edges
to be references to items in verts.
When I try to create a Graph that way I get an error message, here's an example:
fn example() -> Graph<'a> {
let verts = vec![Vertex(0.0, 0.0, 0.0), Vertex(1.0, 1.0, 1.0)];
let edges = vec![(&verts[0], &verts[1])];
Graph { verts, edges }
}
wich results in the following error message:
E0515: cannot return value referencing local variable `verts` returns a value referencing data owned by the current function
and it makes sense, to some extend I get what the error message means, and my code obviously doesn't clearly communicate it's intended purposes.
The compiler really isn't to blame for this but I'm quite glad I caught this error, for I have no idea how I'd solve it.
I am really looking forward to get better at rust thanks to this oppertunity, for now I'm stuck, could you help me out?
Thanks a lot for reading this far, I know my english can be far from good from time to time, really appreciate that you've made it through.
Ps: The E0515 message looks like this:
Summary
Cannot return value that references local variable
Local variables, function parameters and temporaries are all dropped before the
end of the function body. So a reference to them cannot be returned.
#![feature(nll)]
fn get_dangling_reference() -> &'static i32 {
let x = 0;
&x
}
#![feature(nll)]
use std::slice::Iter;
fn get_dangling_iterator<'a>() -> Iter<'a, i32> {
let v = vec![1, 2, 3];
v.iter()
}
Consider returning an owned value instead:
use std::vec::IntoIter;
fn get_integer() -> i32 {
let x = 0;
x
}
fn get_owned_iterator() -> IntoIter<i32> {
let v = vec![1, 2, 3];
v.into_iter()
}