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 ,
| |_________^ 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 ,
| |_________^ 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 ,
| |_________^ 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 ,
| |_________^ 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

143k11268399




143k11268399










asked Nov 8 at 17:10









James May

432719




432719




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

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

Node.js puppeteer - Use values from array in a loop to cycle through pages