temporary value does not live long enough at custom struct [duplicate]











up vote
0
down vote

favorite













This question already has an answer here:




  • Is there any way to return a reference to a variable created in a function?

    2 answers



  • What is the difference between iter and into_iter?

    2 answers



  • Cannot move out of borrowed content

    1 answer




I'm trying to parse an array with customer structures from JSON using Serde. I simplified the example to reproduce the problem, but the idea is the same - I have vector with customer structs:



#[derive(Debug)]
struct X {
x: String,
y: String,
}

fn get_d<'a>() -> Vec<&'a X> {
let mut y: Vec<&X> = vec!;
let x = vec![
&X {
x: String::new(),
y: String::new(),
},
&X {
x: String::new(),
y: String::new(),
},
];
for i in x.iter() {
y.push(i);
}

y
}

fn main() {
let d = get_d();
println!("{:?}", d);
}


I got the error:



error[E0597]: borrowed value does not live long enough
--> src/main.rs:10:10
|
10 | &X {
| __________^
11 | | x: String::new(),
12 | | y: String::new(),
13 | | },
| |_________^ temporary value does not live long enough
...
18 | ];
| - temporary value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 7:10...
--> src/main.rs:7:10
|
7 | fn get_d<'a>() -> Vec<&'a X> {
| ^^
= note: consider using a `let` binding to increase its lifetime


If I change String to u16 at x and y fields in the X struct then it works. The idea is I want to return some part of the vector from the function, nevermind values or pointers.



If I try to return a vector of values I get:



14 |         y.push(*i);
| ^^ cannot move out of borrowed content









share|improve this question















marked as duplicate by Shepmaster rust
Users with the  rust badge can single-handedly close rust questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 8 at 18:15


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Return a vector of values
    – Shepmaster
    Nov 8 at 18:16










  • I'd say the real question here is: "Why does it work when replacing the Strings with u16s". I would expect the references to the X temporaries to be too short-lived too…
    – Jmb
    Nov 9 at 7:39










  • While the question has an answer it does not highlight your mistake, that was creating a vector of pointers and allocate the pointers inside the get_d function. You should instead move the structs out or allocate them on the heap using Box. fn get_d() -> Vec<X> { vec![ X { x: String::new(), y: String::new(), }, X { x: String::new(), y: String::new(), }, ]; }
    – Eduardo Braun
    Nov 9 at 10:29

















up vote
0
down vote

favorite













This question already has an answer here:




  • Is there any way to return a reference to a variable created in a function?

    2 answers



  • What is the difference between iter and into_iter?

    2 answers



  • Cannot move out of borrowed content

    1 answer




I'm trying to parse an array with customer structures from JSON using Serde. I simplified the example to reproduce the problem, but the idea is the same - I have vector with customer structs:



#[derive(Debug)]
struct X {
x: String,
y: String,
}

fn get_d<'a>() -> Vec<&'a X> {
let mut y: Vec<&X> = vec!;
let x = vec![
&X {
x: String::new(),
y: String::new(),
},
&X {
x: String::new(),
y: String::new(),
},
];
for i in x.iter() {
y.push(i);
}

y
}

fn main() {
let d = get_d();
println!("{:?}", d);
}


I got the error:



error[E0597]: borrowed value does not live long enough
--> src/main.rs:10:10
|
10 | &X {
| __________^
11 | | x: String::new(),
12 | | y: String::new(),
13 | | },
| |_________^ temporary value does not live long enough
...
18 | ];
| - temporary value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 7:10...
--> src/main.rs:7:10
|
7 | fn get_d<'a>() -> Vec<&'a X> {
| ^^
= note: consider using a `let` binding to increase its lifetime


If I change String to u16 at x and y fields in the X struct then it works. The idea is I want to return some part of the vector from the function, nevermind values or pointers.



If I try to return a vector of values I get:



14 |         y.push(*i);
| ^^ cannot move out of borrowed content









share|improve this question















marked as duplicate by Shepmaster rust
Users with the  rust badge can single-handedly close rust questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 8 at 18:15


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Return a vector of values
    – Shepmaster
    Nov 8 at 18:16










  • I'd say the real question here is: "Why does it work when replacing the Strings with u16s". I would expect the references to the X temporaries to be too short-lived too…
    – Jmb
    Nov 9 at 7:39










  • While the question has an answer it does not highlight your mistake, that was creating a vector of pointers and allocate the pointers inside the get_d function. You should instead move the structs out or allocate them on the heap using Box. fn get_d() -> Vec<X> { vec![ X { x: String::new(), y: String::new(), }, X { x: String::new(), y: String::new(), }, ]; }
    – Eduardo Braun
    Nov 9 at 10:29















up vote
0
down vote

favorite









up vote
0
down vote

favorite












This question already has an answer here:




  • Is there any way to return a reference to a variable created in a function?

    2 answers



  • What is the difference between iter and into_iter?

    2 answers



  • Cannot move out of borrowed content

    1 answer




I'm trying to parse an array with customer structures from JSON using Serde. I simplified the example to reproduce the problem, but the idea is the same - I have vector with customer structs:



#[derive(Debug)]
struct X {
x: String,
y: String,
}

fn get_d<'a>() -> Vec<&'a X> {
let mut y: Vec<&X> = vec!;
let x = vec![
&X {
x: String::new(),
y: String::new(),
},
&X {
x: String::new(),
y: String::new(),
},
];
for i in x.iter() {
y.push(i);
}

y
}

fn main() {
let d = get_d();
println!("{:?}", d);
}


I got the error:



error[E0597]: borrowed value does not live long enough
--> src/main.rs:10:10
|
10 | &X {
| __________^
11 | | x: String::new(),
12 | | y: String::new(),
13 | | },
| |_________^ temporary value does not live long enough
...
18 | ];
| - temporary value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 7:10...
--> src/main.rs:7:10
|
7 | fn get_d<'a>() -> Vec<&'a X> {
| ^^
= note: consider using a `let` binding to increase its lifetime


If I change String to u16 at x and y fields in the X struct then it works. The idea is I want to return some part of the vector from the function, nevermind values or pointers.



If I try to return a vector of values I get:



14 |         y.push(*i);
| ^^ cannot move out of borrowed content









share|improve this question
















This question already has an answer here:




  • Is there any way to return a reference to a variable created in a function?

    2 answers



  • What is the difference between iter and into_iter?

    2 answers



  • Cannot move out of borrowed content

    1 answer




I'm trying to parse an array with customer structures from JSON using Serde. I simplified the example to reproduce the problem, but the idea is the same - I have vector with customer structs:



#[derive(Debug)]
struct X {
x: String,
y: String,
}

fn get_d<'a>() -> Vec<&'a X> {
let mut y: Vec<&X> = vec!;
let x = vec![
&X {
x: String::new(),
y: String::new(),
},
&X {
x: String::new(),
y: String::new(),
},
];
for i in x.iter() {
y.push(i);
}

y
}

fn main() {
let d = get_d();
println!("{:?}", d);
}


I got the error:



error[E0597]: borrowed value does not live long enough
--> src/main.rs:10:10
|
10 | &X {
| __________^
11 | | x: String::new(),
12 | | y: String::new(),
13 | | },
| |_________^ temporary value does not live long enough
...
18 | ];
| - temporary value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 7:10...
--> src/main.rs:7:10
|
7 | fn get_d<'a>() -> Vec<&'a X> {
| ^^
= note: consider using a `let` binding to increase its lifetime


If I change String to u16 at x and y fields in the X struct then it works. The idea is I want to return some part of the vector from the function, nevermind values or pointers.



If I try to return a vector of values I get:



14 |         y.push(*i);
| ^^ cannot move out of borrowed content




This question already has an answer here:




  • Is there any way to return a reference to a variable created in a function?

    2 answers



  • What is the difference between iter and into_iter?

    2 answers



  • Cannot move out of borrowed content

    1 answer








rust






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 18:15









Shepmaster

145k11274408




145k11274408










asked Nov 8 at 17:10









James May

442719




442719




marked as duplicate by Shepmaster rust
Users with the  rust badge can single-handedly close rust questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 8 at 18:15


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Shepmaster rust
Users with the  rust badge can single-handedly close rust questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 8 at 18:15


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Return a vector of values
    – Shepmaster
    Nov 8 at 18:16










  • I'd say the real question here is: "Why does it work when replacing the Strings with u16s". I would expect the references to the X temporaries to be too short-lived too…
    – Jmb
    Nov 9 at 7:39










  • While the question has an answer it does not highlight your mistake, that was creating a vector of pointers and allocate the pointers inside the get_d function. You should instead move the structs out or allocate them on the heap using Box. fn get_d() -> Vec<X> { vec![ X { x: String::new(), y: String::new(), }, X { x: String::new(), y: String::new(), }, ]; }
    – Eduardo Braun
    Nov 9 at 10:29




















  • Return a vector of values
    – Shepmaster
    Nov 8 at 18:16










  • I'd say the real question here is: "Why does it work when replacing the Strings with u16s". I would expect the references to the X temporaries to be too short-lived too…
    – Jmb
    Nov 9 at 7:39










  • While the question has an answer it does not highlight your mistake, that was creating a vector of pointers and allocate the pointers inside the get_d function. You should instead move the structs out or allocate them on the heap using Box. fn get_d() -> Vec<X> { vec![ X { x: String::new(), y: String::new(), }, X { x: String::new(), y: String::new(), }, ]; }
    – Eduardo Braun
    Nov 9 at 10:29


















Return a vector of values
– Shepmaster
Nov 8 at 18:16




Return a vector of values
– Shepmaster
Nov 8 at 18:16












I'd say the real question here is: "Why does it work when replacing the Strings with u16s". I would expect the references to the X temporaries to be too short-lived too…
– Jmb
Nov 9 at 7:39




I'd say the real question here is: "Why does it work when replacing the Strings with u16s". I would expect the references to the X temporaries to be too short-lived too…
– Jmb
Nov 9 at 7:39












While the question has an answer it does not highlight your mistake, that was creating a vector of pointers and allocate the pointers inside the get_d function. You should instead move the structs out or allocate them on the heap using Box. fn get_d() -> Vec<X> { vec![ X { x: String::new(), y: String::new(), }, X { x: String::new(), y: String::new(), }, ]; }
– Eduardo Braun
Nov 9 at 10:29






While the question has an answer it does not highlight your mistake, that was creating a vector of pointers and allocate the pointers inside the get_d function. You should instead move the structs out or allocate them on the heap using Box. fn get_d() -> Vec<X> { vec![ X { x: String::new(), y: String::new(), }, X { x: String::new(), y: String::new(), }, ]; }
– Eduardo Braun
Nov 9 at 10:29



















active

oldest

votes






















active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes

這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings