Is it possible to take temporary ownership of a struct field? [duplicate]










0
















This question already has an answer here:



  • Temporarily move out of borrowed content

    2 answers



  • How can I swap in a new value for a field in a mutable reference to a structure?

    2 answers



I have an immutable data structure, and an update function that takes ownership of the data structure and returns a new data structure:



enum Immutable 
Item(i32)


fn update(imm: Immutable) -> Immutable
match imm
Immutable::Item(x) => Immutable::Item(x + 1)




I need to store the data structure in a mutable field of a container:



struct State 
item: Immutable



I want to write an imperative update function for State that calls the function updater:



fn update_mut(st: &mut State) -> () 
let mut owned = Immutable::Item(42); // junk
std::mem::swap(&mut st.item, &mut owned);
st.item = update(owned);



This code works, but it seems sily to use mem::swap and allocate a junk object. I would really like to write:



fn update_mut_type_error(st: &mut State) -> () 
let mut owned = Immutable::Item(42); // junk
std::mem::swap(&mut st.item, &mut owned);
st.item = update(st.item); // type error



Is there any way to address this? Or, do I have to use mem::swap here, even though it seems spurious.



Example on Rust Playground










share|improve this question















marked as duplicate by loganfsmyth, hellow, E_net4, trentcl, 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 16 '18 at 2:02


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.


















  • Neither of your example links work FYI. You probably forgot to make sharable links.

    – loganfsmyth
    Nov 15 '18 at 4:05






  • 3





    The case to consider is, what would happen if update were to panic? You'd have transferred st.item to update, so what value would be in st.item after the panic? It needs to be something or else the you're leaving your State object in an invalid state. Also good reading: smallcultfollowing.com/babysteps/blog/2018/11/10/…

    – loganfsmyth
    Nov 15 '18 at 4:11






  • 3





    The difference to the question tagged as duplicate is that the update() function requires to temporarily take ownership of st.item while the new value is build. This means the solutions based on std::mem::replace() and std::mem::swap() in the first answer won't work as written there. The best solution would be to change the interface of update() to take a reference instead, if possible. If not, I recommend reading this blog post on the subject.

    – Sven Marnach
    Nov 15 '18 at 12:11











  • Thanks, @SvenMarnach. That blog post has essentially the same example. mem::swap is slightly cleaner than my solution.

    – Arjun Guha
    Nov 15 '18 at 12:23















0
















This question already has an answer here:



  • Temporarily move out of borrowed content

    2 answers



  • How can I swap in a new value for a field in a mutable reference to a structure?

    2 answers



I have an immutable data structure, and an update function that takes ownership of the data structure and returns a new data structure:



enum Immutable 
Item(i32)


fn update(imm: Immutable) -> Immutable
match imm
Immutable::Item(x) => Immutable::Item(x + 1)




I need to store the data structure in a mutable field of a container:



struct State 
item: Immutable



I want to write an imperative update function for State that calls the function updater:



fn update_mut(st: &mut State) -> () 
let mut owned = Immutable::Item(42); // junk
std::mem::swap(&mut st.item, &mut owned);
st.item = update(owned);



This code works, but it seems sily to use mem::swap and allocate a junk object. I would really like to write:



fn update_mut_type_error(st: &mut State) -> () 
let mut owned = Immutable::Item(42); // junk
std::mem::swap(&mut st.item, &mut owned);
st.item = update(st.item); // type error



Is there any way to address this? Or, do I have to use mem::swap here, even though it seems spurious.



Example on Rust Playground










share|improve this question















marked as duplicate by loganfsmyth, hellow, E_net4, trentcl, 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 16 '18 at 2:02


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.


















  • Neither of your example links work FYI. You probably forgot to make sharable links.

    – loganfsmyth
    Nov 15 '18 at 4:05






  • 3





    The case to consider is, what would happen if update were to panic? You'd have transferred st.item to update, so what value would be in st.item after the panic? It needs to be something or else the you're leaving your State object in an invalid state. Also good reading: smallcultfollowing.com/babysteps/blog/2018/11/10/…

    – loganfsmyth
    Nov 15 '18 at 4:11






  • 3





    The difference to the question tagged as duplicate is that the update() function requires to temporarily take ownership of st.item while the new value is build. This means the solutions based on std::mem::replace() and std::mem::swap() in the first answer won't work as written there. The best solution would be to change the interface of update() to take a reference instead, if possible. If not, I recommend reading this blog post on the subject.

    – Sven Marnach
    Nov 15 '18 at 12:11











  • Thanks, @SvenMarnach. That blog post has essentially the same example. mem::swap is slightly cleaner than my solution.

    – Arjun Guha
    Nov 15 '18 at 12:23













0












0








0









This question already has an answer here:



  • Temporarily move out of borrowed content

    2 answers



  • How can I swap in a new value for a field in a mutable reference to a structure?

    2 answers



I have an immutable data structure, and an update function that takes ownership of the data structure and returns a new data structure:



enum Immutable 
Item(i32)


fn update(imm: Immutable) -> Immutable
match imm
Immutable::Item(x) => Immutable::Item(x + 1)




I need to store the data structure in a mutable field of a container:



struct State 
item: Immutable



I want to write an imperative update function for State that calls the function updater:



fn update_mut(st: &mut State) -> () 
let mut owned = Immutable::Item(42); // junk
std::mem::swap(&mut st.item, &mut owned);
st.item = update(owned);



This code works, but it seems sily to use mem::swap and allocate a junk object. I would really like to write:



fn update_mut_type_error(st: &mut State) -> () 
let mut owned = Immutable::Item(42); // junk
std::mem::swap(&mut st.item, &mut owned);
st.item = update(st.item); // type error



Is there any way to address this? Or, do I have to use mem::swap here, even though it seems spurious.



Example on Rust Playground










share|improve this question

















This question already has an answer here:



  • Temporarily move out of borrowed content

    2 answers



  • How can I swap in a new value for a field in a mutable reference to a structure?

    2 answers



I have an immutable data structure, and an update function that takes ownership of the data structure and returns a new data structure:



enum Immutable 
Item(i32)


fn update(imm: Immutable) -> Immutable
match imm
Immutable::Item(x) => Immutable::Item(x + 1)




I need to store the data structure in a mutable field of a container:



struct State 
item: Immutable



I want to write an imperative update function for State that calls the function updater:



fn update_mut(st: &mut State) -> () 
let mut owned = Immutable::Item(42); // junk
std::mem::swap(&mut st.item, &mut owned);
st.item = update(owned);



This code works, but it seems sily to use mem::swap and allocate a junk object. I would really like to write:



fn update_mut_type_error(st: &mut State) -> () 
let mut owned = Immutable::Item(42); // junk
std::mem::swap(&mut st.item, &mut owned);
st.item = update(st.item); // type error



Is there any way to address this? Or, do I have to use mem::swap here, even though it seems spurious.



Example on Rust Playground





This question already has an answer here:



  • Temporarily move out of borrowed content

    2 answers



  • How can I swap in a new value for a field in a mutable reference to a structure?

    2 answers







rust






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 15:23







Arjun Guha

















asked Nov 15 '18 at 3:57









Arjun GuhaArjun Guha

31139




31139




marked as duplicate by loganfsmyth, hellow, E_net4, trentcl, 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 16 '18 at 2:02


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 loganfsmyth, hellow, E_net4, trentcl, 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 16 '18 at 2:02


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.














  • Neither of your example links work FYI. You probably forgot to make sharable links.

    – loganfsmyth
    Nov 15 '18 at 4:05






  • 3





    The case to consider is, what would happen if update were to panic? You'd have transferred st.item to update, so what value would be in st.item after the panic? It needs to be something or else the you're leaving your State object in an invalid state. Also good reading: smallcultfollowing.com/babysteps/blog/2018/11/10/…

    – loganfsmyth
    Nov 15 '18 at 4:11






  • 3





    The difference to the question tagged as duplicate is that the update() function requires to temporarily take ownership of st.item while the new value is build. This means the solutions based on std::mem::replace() and std::mem::swap() in the first answer won't work as written there. The best solution would be to change the interface of update() to take a reference instead, if possible. If not, I recommend reading this blog post on the subject.

    – Sven Marnach
    Nov 15 '18 at 12:11











  • Thanks, @SvenMarnach. That blog post has essentially the same example. mem::swap is slightly cleaner than my solution.

    – Arjun Guha
    Nov 15 '18 at 12:23

















  • Neither of your example links work FYI. You probably forgot to make sharable links.

    – loganfsmyth
    Nov 15 '18 at 4:05






  • 3





    The case to consider is, what would happen if update were to panic? You'd have transferred st.item to update, so what value would be in st.item after the panic? It needs to be something or else the you're leaving your State object in an invalid state. Also good reading: smallcultfollowing.com/babysteps/blog/2018/11/10/…

    – loganfsmyth
    Nov 15 '18 at 4:11






  • 3





    The difference to the question tagged as duplicate is that the update() function requires to temporarily take ownership of st.item while the new value is build. This means the solutions based on std::mem::replace() and std::mem::swap() in the first answer won't work as written there. The best solution would be to change the interface of update() to take a reference instead, if possible. If not, I recommend reading this blog post on the subject.

    – Sven Marnach
    Nov 15 '18 at 12:11











  • Thanks, @SvenMarnach. That blog post has essentially the same example. mem::swap is slightly cleaner than my solution.

    – Arjun Guha
    Nov 15 '18 at 12:23
















Neither of your example links work FYI. You probably forgot to make sharable links.

– loganfsmyth
Nov 15 '18 at 4:05





Neither of your example links work FYI. You probably forgot to make sharable links.

– loganfsmyth
Nov 15 '18 at 4:05




3




3





The case to consider is, what would happen if update were to panic? You'd have transferred st.item to update, so what value would be in st.item after the panic? It needs to be something or else the you're leaving your State object in an invalid state. Also good reading: smallcultfollowing.com/babysteps/blog/2018/11/10/…

– loganfsmyth
Nov 15 '18 at 4:11





The case to consider is, what would happen if update were to panic? You'd have transferred st.item to update, so what value would be in st.item after the panic? It needs to be something or else the you're leaving your State object in an invalid state. Also good reading: smallcultfollowing.com/babysteps/blog/2018/11/10/…

– loganfsmyth
Nov 15 '18 at 4:11




3




3





The difference to the question tagged as duplicate is that the update() function requires to temporarily take ownership of st.item while the new value is build. This means the solutions based on std::mem::replace() and std::mem::swap() in the first answer won't work as written there. The best solution would be to change the interface of update() to take a reference instead, if possible. If not, I recommend reading this blog post on the subject.

– Sven Marnach
Nov 15 '18 at 12:11





The difference to the question tagged as duplicate is that the update() function requires to temporarily take ownership of st.item while the new value is build. This means the solutions based on std::mem::replace() and std::mem::swap() in the first answer won't work as written there. The best solution would be to change the interface of update() to take a reference instead, if possible. If not, I recommend reading this blog post on the subject.

– Sven Marnach
Nov 15 '18 at 12:11













Thanks, @SvenMarnach. That blog post has essentially the same example. mem::swap is slightly cleaner than my solution.

– Arjun Guha
Nov 15 '18 at 12:23





Thanks, @SvenMarnach. That blog post has essentially the same example. mem::swap is slightly cleaner than my solution.

– Arjun Guha
Nov 15 '18 at 12:23












1 Answer
1






active

oldest

votes


















-2














As Sven points out, this question is answered here:



http://smallcultfollowing.com/babysteps/blog/2018/11/10/after-nll-moving-from-borrowed-data-and-the-sentinel-pattern/






share|improve this answer























  • Please don't post link-only answers. If you agree that your question is answered at How can I swap in a new value for a field in a mutable reference to a structure?, you can close it yourself -- there should be a "close" button underneath the post. If you don't see that, you might not have enough reputation; you don't need to do anything. If you don't believe your question is substantially the same as that one, you can answer it yourself, but...

    – trentcl
    Nov 15 '18 at 22:11











  • ... don't just post a link. Copy and quote the relevant portions of the article, and add any explanation that might be necessary to make your answer stand alone. Blogs can go down; Stack Overflow is interested in answers (and questions) that don't depend on external links.

    – trentcl
    Nov 15 '18 at 22:13


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-2














As Sven points out, this question is answered here:



http://smallcultfollowing.com/babysteps/blog/2018/11/10/after-nll-moving-from-borrowed-data-and-the-sentinel-pattern/






share|improve this answer























  • Please don't post link-only answers. If you agree that your question is answered at How can I swap in a new value for a field in a mutable reference to a structure?, you can close it yourself -- there should be a "close" button underneath the post. If you don't see that, you might not have enough reputation; you don't need to do anything. If you don't believe your question is substantially the same as that one, you can answer it yourself, but...

    – trentcl
    Nov 15 '18 at 22:11











  • ... don't just post a link. Copy and quote the relevant portions of the article, and add any explanation that might be necessary to make your answer stand alone. Blogs can go down; Stack Overflow is interested in answers (and questions) that don't depend on external links.

    – trentcl
    Nov 15 '18 at 22:13
















-2














As Sven points out, this question is answered here:



http://smallcultfollowing.com/babysteps/blog/2018/11/10/after-nll-moving-from-borrowed-data-and-the-sentinel-pattern/






share|improve this answer























  • Please don't post link-only answers. If you agree that your question is answered at How can I swap in a new value for a field in a mutable reference to a structure?, you can close it yourself -- there should be a "close" button underneath the post. If you don't see that, you might not have enough reputation; you don't need to do anything. If you don't believe your question is substantially the same as that one, you can answer it yourself, but...

    – trentcl
    Nov 15 '18 at 22:11











  • ... don't just post a link. Copy and quote the relevant portions of the article, and add any explanation that might be necessary to make your answer stand alone. Blogs can go down; Stack Overflow is interested in answers (and questions) that don't depend on external links.

    – trentcl
    Nov 15 '18 at 22:13














-2












-2








-2







As Sven points out, this question is answered here:



http://smallcultfollowing.com/babysteps/blog/2018/11/10/after-nll-moving-from-borrowed-data-and-the-sentinel-pattern/






share|improve this answer













As Sven points out, this question is answered here:



http://smallcultfollowing.com/babysteps/blog/2018/11/10/after-nll-moving-from-borrowed-data-and-the-sentinel-pattern/







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 15:24









Arjun GuhaArjun Guha

31139




31139












  • Please don't post link-only answers. If you agree that your question is answered at How can I swap in a new value for a field in a mutable reference to a structure?, you can close it yourself -- there should be a "close" button underneath the post. If you don't see that, you might not have enough reputation; you don't need to do anything. If you don't believe your question is substantially the same as that one, you can answer it yourself, but...

    – trentcl
    Nov 15 '18 at 22:11











  • ... don't just post a link. Copy and quote the relevant portions of the article, and add any explanation that might be necessary to make your answer stand alone. Blogs can go down; Stack Overflow is interested in answers (and questions) that don't depend on external links.

    – trentcl
    Nov 15 '18 at 22:13


















  • Please don't post link-only answers. If you agree that your question is answered at How can I swap in a new value for a field in a mutable reference to a structure?, you can close it yourself -- there should be a "close" button underneath the post. If you don't see that, you might not have enough reputation; you don't need to do anything. If you don't believe your question is substantially the same as that one, you can answer it yourself, but...

    – trentcl
    Nov 15 '18 at 22:11











  • ... don't just post a link. Copy and quote the relevant portions of the article, and add any explanation that might be necessary to make your answer stand alone. Blogs can go down; Stack Overflow is interested in answers (and questions) that don't depend on external links.

    – trentcl
    Nov 15 '18 at 22:13

















Please don't post link-only answers. If you agree that your question is answered at How can I swap in a new value for a field in a mutable reference to a structure?, you can close it yourself -- there should be a "close" button underneath the post. If you don't see that, you might not have enough reputation; you don't need to do anything. If you don't believe your question is substantially the same as that one, you can answer it yourself, but...

– trentcl
Nov 15 '18 at 22:11





Please don't post link-only answers. If you agree that your question is answered at How can I swap in a new value for a field in a mutable reference to a structure?, you can close it yourself -- there should be a "close" button underneath the post. If you don't see that, you might not have enough reputation; you don't need to do anything. If you don't believe your question is substantially the same as that one, you can answer it yourself, but...

– trentcl
Nov 15 '18 at 22:11













... don't just post a link. Copy and quote the relevant portions of the article, and add any explanation that might be necessary to make your answer stand alone. Blogs can go down; Stack Overflow is interested in answers (and questions) that don't depend on external links.

– trentcl
Nov 15 '18 at 22:13






... don't just post a link. Copy and quote the relevant portions of the article, and add any explanation that might be necessary to make your answer stand alone. Blogs can go down; Stack Overflow is interested in answers (and questions) that don't depend on external links.

– trentcl
Nov 15 '18 at 22:13






Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20