What would be a better way to implement .pop() in my single linked list in Rust? The 2019 Stack Overflow Developer Survey Results Are InTwo ways of implementing a linked list: which is better?Rust: How to implement linked list?What are the Rust types denoted with a single apostrophe?How would you implement a bi-directional linked list in Rust?The best way to print a singly linked list in rustWhats wrong with this c code(linked list implementation)?What is the use of the position interface in the Linked list implementations?Singly-Linked List in RustCreating a single linked listWhat is wrong with my Singly Linked List implementation?

Accepted by European university, rejected by all American ones I applied to? Possible reasons?

What to do when moving next to a bird sanctuary with a loosely-domesticated cat?

Output the Arecibo Message

Does adding complexity mean a more secure cipher?

Can you cast a spell on someone in the Ethereal Plane, if you are on the Material Plane and have the True Seeing spell active?

How do I free up internal storage if I don't have any apps downloaded?

Getting crown tickets for Statue of Liberty

For what reasons would an animal species NOT cross a *horizontal* land bridge?

Alternative to の

What do I do when my TA workload is more than expected?

Why don't hard Brexiteers insist on a hard border to prevent illegal immigration after Brexit?

Is bread bad for ducks?

How to charge AirPods to keep battery healthy?

Cooking pasta in a water boiler

What is the meaning of Triage in Cybersec world?

If climate change impact can be observed in nature, has that had any effect on rural, i.e. farming community, perception of the scientific consensus?

Deal with toxic manager when you can't quit

If I score a critical hit on an 18 or higher, what are my chances of getting a critical hit if I roll 3d20?

What is the most efficient way to store a numeric range?

What is the motivation for a law requiring 2 parties to consent for recording a conversation

What does もの mean in this sentence?

Short story: man watches girlfriend's spaceship entering a 'black hole' (?) forever

How to support a colleague who finds meetings extremely tiring?

Is it ok to offer lower paid work as a trial period before negotiating for a full-time job?



What would be a better way to implement .pop() in my single linked list in Rust?



The 2019 Stack Overflow Developer Survey Results Are InTwo ways of implementing a linked list: which is better?Rust: How to implement linked list?What are the Rust types denoted with a single apostrophe?How would you implement a bi-directional linked list in Rust?The best way to print a singly linked list in rustWhats wrong with this c code(linked list implementation)?What is the use of the position interface in the Linked list implementations?Singly-Linked List in RustCreating a single linked listWhat is wrong with my Singly Linked List implementation?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








2















I've implemented my own version of a singly linked list in Rust as one of the challenges for me to learn it, and I'm satisfied with everything I have there except for the .pop() method. Using 2 while loops is very ugly and inefficient, but I found no other way to overcome the problem of setting the node at the index len() - 2 to None (popping the list), and using the data from the node at the index len() - 1 for the Some(data) return value (returns the element that was popped).



GitHub Link



pub struct SimpleLinkedList<T> 
head: Option<Box<Node<T>>>,


struct Node<T>
data: T,
next: Option<Box<Node<T>>>,


impl<T> Default for SimpleLinkedList<T>
fn default() -> Self
SimpleLinkedList head: None



impl<T: Copy> Clone for SimpleLinkedList<T>
fn clone(&self) -> SimpleLinkedList<T>
let mut out: SimpleLinkedList<T> = SimpleLinkedList::new();
let mut cur = &self.head;
while let Some(node) = cur
cur = &node.next;
out.push(node.data)

out



impl<T> SimpleLinkedList<T>
pub fn new() -> Self
Default::default()


pub fn len(&self) -> usize
let mut c = 0;
let mut cur = &self.head;
while let Some(node) = cur
cur = &node.next;
c += 1;

c


pub fn is_empty(&self) -> bool
self.len() == 0


pub fn push(&mut self, _element: T)
let mut cur = &mut self.head;
match cur
Some(_) =>
while let Some(node) = cur
cur = &mut node.next;


None => (),

*cur = Some(Box::from(Node
data: _element,
next: None,
));


pub fn pop(&mut self) -> Option<T>
where
T: Copy,

let length = &self.len();
let mut cur = &mut self.head;
let mut out = None;
match cur
Some(_) if *length > 1usize =>
let mut c = 0usize;
while let Some(node) = cur
cur = &mut node.next;
if c >= length - 1
out = Some(node.data);
break;

c += 1;


c = 0usize;
cur = &mut self.head;
while let Some(node) = cur
cur = &mut node.next;
if c == length - 2
break;

c += 1;


Some(node) => out = Some(node.data),
None => (),

*cur = None;
out


pub fn peek(&self) -> Option<&T>
let cur = &self.head;
match cur
Some(node) => Some(&node.data),
None => None,




impl<T: Copy> SimpleLinkedList<T>
pub fn rev(&self) -> SimpleLinkedList<T>
let mut clone = self.clone();
let mut out: SimpleLinkedList<T> = SimpleLinkedList::new();
while let Some(val) = clone.pop()
out.push(val)

out



impl<'a, T: Copy> From<&'a [T]> for SimpleLinkedList<T>
fn from(_item: &[T]) -> Self
let mut out: SimpleLinkedList<T> = SimpleLinkedList::new();
for &e in _item.iter()
out.push(e);

out



impl<T> Into<Vec<T>> for SimpleLinkedList<T>
fn into(self) -> Vec<T>
let mut out: Vec<T> = Vec::new();
let mut cur = self.head;
while let Some(node) = cur
cur = node.next;
out.push(node.data)

out











share|improve this question



















  • 1





    linked list is the functional way to represent data. Rust borrow checker will make your life complicated if you try to use imperatif to implemente it in Rust. See, codereview.stackexchange.com/questions/207418/…. With functionnal style all is cleaner, of course maybe not faster, rust linked list require unsafe I think to make it fast (and readable).

    – Stargateur
    Mar 8 at 11:24












  • I'm voting to close this question as off-topic because already answer here

    – Stargateur
    Mar 8 at 11:26






  • 4





    Near-mandatory reading: Learning Rust with entirely too many linked lists

    – E_net4
    Mar 8 at 11:27











  • @E_net4 That looks like an awesome read! I will certainly make time to read it in the upcoming days

    – scorpion9979
    Mar 8 at 11:51






  • 1





    Thanks for posting a link that included your tests.

    – jelford
    Mar 9 at 18:36

















2















I've implemented my own version of a singly linked list in Rust as one of the challenges for me to learn it, and I'm satisfied with everything I have there except for the .pop() method. Using 2 while loops is very ugly and inefficient, but I found no other way to overcome the problem of setting the node at the index len() - 2 to None (popping the list), and using the data from the node at the index len() - 1 for the Some(data) return value (returns the element that was popped).



GitHub Link



pub struct SimpleLinkedList<T> 
head: Option<Box<Node<T>>>,


struct Node<T>
data: T,
next: Option<Box<Node<T>>>,


impl<T> Default for SimpleLinkedList<T>
fn default() -> Self
SimpleLinkedList head: None



impl<T: Copy> Clone for SimpleLinkedList<T>
fn clone(&self) -> SimpleLinkedList<T>
let mut out: SimpleLinkedList<T> = SimpleLinkedList::new();
let mut cur = &self.head;
while let Some(node) = cur
cur = &node.next;
out.push(node.data)

out



impl<T> SimpleLinkedList<T>
pub fn new() -> Self
Default::default()


pub fn len(&self) -> usize
let mut c = 0;
let mut cur = &self.head;
while let Some(node) = cur
cur = &node.next;
c += 1;

c


pub fn is_empty(&self) -> bool
self.len() == 0


pub fn push(&mut self, _element: T)
let mut cur = &mut self.head;
match cur
Some(_) =>
while let Some(node) = cur
cur = &mut node.next;


None => (),

*cur = Some(Box::from(Node
data: _element,
next: None,
));


pub fn pop(&mut self) -> Option<T>
where
T: Copy,

let length = &self.len();
let mut cur = &mut self.head;
let mut out = None;
match cur
Some(_) if *length > 1usize =>
let mut c = 0usize;
while let Some(node) = cur
cur = &mut node.next;
if c >= length - 1
out = Some(node.data);
break;

c += 1;


c = 0usize;
cur = &mut self.head;
while let Some(node) = cur
cur = &mut node.next;
if c == length - 2
break;

c += 1;


Some(node) => out = Some(node.data),
None => (),

*cur = None;
out


pub fn peek(&self) -> Option<&T>
let cur = &self.head;
match cur
Some(node) => Some(&node.data),
None => None,




impl<T: Copy> SimpleLinkedList<T>
pub fn rev(&self) -> SimpleLinkedList<T>
let mut clone = self.clone();
let mut out: SimpleLinkedList<T> = SimpleLinkedList::new();
while let Some(val) = clone.pop()
out.push(val)

out



impl<'a, T: Copy> From<&'a [T]> for SimpleLinkedList<T>
fn from(_item: &[T]) -> Self
let mut out: SimpleLinkedList<T> = SimpleLinkedList::new();
for &e in _item.iter()
out.push(e);

out



impl<T> Into<Vec<T>> for SimpleLinkedList<T>
fn into(self) -> Vec<T>
let mut out: Vec<T> = Vec::new();
let mut cur = self.head;
while let Some(node) = cur
cur = node.next;
out.push(node.data)

out











share|improve this question



















  • 1





    linked list is the functional way to represent data. Rust borrow checker will make your life complicated if you try to use imperatif to implemente it in Rust. See, codereview.stackexchange.com/questions/207418/…. With functionnal style all is cleaner, of course maybe not faster, rust linked list require unsafe I think to make it fast (and readable).

    – Stargateur
    Mar 8 at 11:24












  • I'm voting to close this question as off-topic because already answer here

    – Stargateur
    Mar 8 at 11:26






  • 4





    Near-mandatory reading: Learning Rust with entirely too many linked lists

    – E_net4
    Mar 8 at 11:27











  • @E_net4 That looks like an awesome read! I will certainly make time to read it in the upcoming days

    – scorpion9979
    Mar 8 at 11:51






  • 1





    Thanks for posting a link that included your tests.

    – jelford
    Mar 9 at 18:36













2












2








2








I've implemented my own version of a singly linked list in Rust as one of the challenges for me to learn it, and I'm satisfied with everything I have there except for the .pop() method. Using 2 while loops is very ugly and inefficient, but I found no other way to overcome the problem of setting the node at the index len() - 2 to None (popping the list), and using the data from the node at the index len() - 1 for the Some(data) return value (returns the element that was popped).



GitHub Link



pub struct SimpleLinkedList<T> 
head: Option<Box<Node<T>>>,


struct Node<T>
data: T,
next: Option<Box<Node<T>>>,


impl<T> Default for SimpleLinkedList<T>
fn default() -> Self
SimpleLinkedList head: None



impl<T: Copy> Clone for SimpleLinkedList<T>
fn clone(&self) -> SimpleLinkedList<T>
let mut out: SimpleLinkedList<T> = SimpleLinkedList::new();
let mut cur = &self.head;
while let Some(node) = cur
cur = &node.next;
out.push(node.data)

out



impl<T> SimpleLinkedList<T>
pub fn new() -> Self
Default::default()


pub fn len(&self) -> usize
let mut c = 0;
let mut cur = &self.head;
while let Some(node) = cur
cur = &node.next;
c += 1;

c


pub fn is_empty(&self) -> bool
self.len() == 0


pub fn push(&mut self, _element: T)
let mut cur = &mut self.head;
match cur
Some(_) =>
while let Some(node) = cur
cur = &mut node.next;


None => (),

*cur = Some(Box::from(Node
data: _element,
next: None,
));


pub fn pop(&mut self) -> Option<T>
where
T: Copy,

let length = &self.len();
let mut cur = &mut self.head;
let mut out = None;
match cur
Some(_) if *length > 1usize =>
let mut c = 0usize;
while let Some(node) = cur
cur = &mut node.next;
if c >= length - 1
out = Some(node.data);
break;

c += 1;


c = 0usize;
cur = &mut self.head;
while let Some(node) = cur
cur = &mut node.next;
if c == length - 2
break;

c += 1;


Some(node) => out = Some(node.data),
None => (),

*cur = None;
out


pub fn peek(&self) -> Option<&T>
let cur = &self.head;
match cur
Some(node) => Some(&node.data),
None => None,




impl<T: Copy> SimpleLinkedList<T>
pub fn rev(&self) -> SimpleLinkedList<T>
let mut clone = self.clone();
let mut out: SimpleLinkedList<T> = SimpleLinkedList::new();
while let Some(val) = clone.pop()
out.push(val)

out



impl<'a, T: Copy> From<&'a [T]> for SimpleLinkedList<T>
fn from(_item: &[T]) -> Self
let mut out: SimpleLinkedList<T> = SimpleLinkedList::new();
for &e in _item.iter()
out.push(e);

out



impl<T> Into<Vec<T>> for SimpleLinkedList<T>
fn into(self) -> Vec<T>
let mut out: Vec<T> = Vec::new();
let mut cur = self.head;
while let Some(node) = cur
cur = node.next;
out.push(node.data)

out











share|improve this question
















I've implemented my own version of a singly linked list in Rust as one of the challenges for me to learn it, and I'm satisfied with everything I have there except for the .pop() method. Using 2 while loops is very ugly and inefficient, but I found no other way to overcome the problem of setting the node at the index len() - 2 to None (popping the list), and using the data from the node at the index len() - 1 for the Some(data) return value (returns the element that was popped).



GitHub Link



pub struct SimpleLinkedList<T> 
head: Option<Box<Node<T>>>,


struct Node<T>
data: T,
next: Option<Box<Node<T>>>,


impl<T> Default for SimpleLinkedList<T>
fn default() -> Self
SimpleLinkedList head: None



impl<T: Copy> Clone for SimpleLinkedList<T>
fn clone(&self) -> SimpleLinkedList<T>
let mut out: SimpleLinkedList<T> = SimpleLinkedList::new();
let mut cur = &self.head;
while let Some(node) = cur
cur = &node.next;
out.push(node.data)

out



impl<T> SimpleLinkedList<T>
pub fn new() -> Self
Default::default()


pub fn len(&self) -> usize
let mut c = 0;
let mut cur = &self.head;
while let Some(node) = cur
cur = &node.next;
c += 1;

c


pub fn is_empty(&self) -> bool
self.len() == 0


pub fn push(&mut self, _element: T)
let mut cur = &mut self.head;
match cur
Some(_) =>
while let Some(node) = cur
cur = &mut node.next;


None => (),

*cur = Some(Box::from(Node
data: _element,
next: None,
));


pub fn pop(&mut self) -> Option<T>
where
T: Copy,

let length = &self.len();
let mut cur = &mut self.head;
let mut out = None;
match cur
Some(_) if *length > 1usize =>
let mut c = 0usize;
while let Some(node) = cur
cur = &mut node.next;
if c >= length - 1
out = Some(node.data);
break;

c += 1;


c = 0usize;
cur = &mut self.head;
while let Some(node) = cur
cur = &mut node.next;
if c == length - 2
break;

c += 1;


Some(node) => out = Some(node.data),
None => (),

*cur = None;
out


pub fn peek(&self) -> Option<&T>
let cur = &self.head;
match cur
Some(node) => Some(&node.data),
None => None,




impl<T: Copy> SimpleLinkedList<T>
pub fn rev(&self) -> SimpleLinkedList<T>
let mut clone = self.clone();
let mut out: SimpleLinkedList<T> = SimpleLinkedList::new();
while let Some(val) = clone.pop()
out.push(val)

out



impl<'a, T: Copy> From<&'a [T]> for SimpleLinkedList<T>
fn from(_item: &[T]) -> Self
let mut out: SimpleLinkedList<T> = SimpleLinkedList::new();
for &e in _item.iter()
out.push(e);

out



impl<T> Into<Vec<T>> for SimpleLinkedList<T>
fn into(self) -> Vec<T>
let mut out: Vec<T> = Vec::new();
let mut cur = self.head;
while let Some(node) = cur
cur = node.next;
out.push(node.data)

out








linked-list rust singly-linked-list






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 12:02









Wesley Wiser

6,77143351




6,77143351










asked Mar 8 at 11:13









scorpion9979scorpion9979

356




356







  • 1





    linked list is the functional way to represent data. Rust borrow checker will make your life complicated if you try to use imperatif to implemente it in Rust. See, codereview.stackexchange.com/questions/207418/…. With functionnal style all is cleaner, of course maybe not faster, rust linked list require unsafe I think to make it fast (and readable).

    – Stargateur
    Mar 8 at 11:24












  • I'm voting to close this question as off-topic because already answer here

    – Stargateur
    Mar 8 at 11:26






  • 4





    Near-mandatory reading: Learning Rust with entirely too many linked lists

    – E_net4
    Mar 8 at 11:27











  • @E_net4 That looks like an awesome read! I will certainly make time to read it in the upcoming days

    – scorpion9979
    Mar 8 at 11:51






  • 1





    Thanks for posting a link that included your tests.

    – jelford
    Mar 9 at 18:36












  • 1





    linked list is the functional way to represent data. Rust borrow checker will make your life complicated if you try to use imperatif to implemente it in Rust. See, codereview.stackexchange.com/questions/207418/…. With functionnal style all is cleaner, of course maybe not faster, rust linked list require unsafe I think to make it fast (and readable).

    – Stargateur
    Mar 8 at 11:24












  • I'm voting to close this question as off-topic because already answer here

    – Stargateur
    Mar 8 at 11:26






  • 4





    Near-mandatory reading: Learning Rust with entirely too many linked lists

    – E_net4
    Mar 8 at 11:27











  • @E_net4 That looks like an awesome read! I will certainly make time to read it in the upcoming days

    – scorpion9979
    Mar 8 at 11:51






  • 1





    Thanks for posting a link that included your tests.

    – jelford
    Mar 9 at 18:36







1




1





linked list is the functional way to represent data. Rust borrow checker will make your life complicated if you try to use imperatif to implemente it in Rust. See, codereview.stackexchange.com/questions/207418/…. With functionnal style all is cleaner, of course maybe not faster, rust linked list require unsafe I think to make it fast (and readable).

– Stargateur
Mar 8 at 11:24






linked list is the functional way to represent data. Rust borrow checker will make your life complicated if you try to use imperatif to implemente it in Rust. See, codereview.stackexchange.com/questions/207418/…. With functionnal style all is cleaner, of course maybe not faster, rust linked list require unsafe I think to make it fast (and readable).

– Stargateur
Mar 8 at 11:24














I'm voting to close this question as off-topic because already answer here

– Stargateur
Mar 8 at 11:26





I'm voting to close this question as off-topic because already answer here

– Stargateur
Mar 8 at 11:26




4




4





Near-mandatory reading: Learning Rust with entirely too many linked lists

– E_net4
Mar 8 at 11:27





Near-mandatory reading: Learning Rust with entirely too many linked lists

– E_net4
Mar 8 at 11:27













@E_net4 That looks like an awesome read! I will certainly make time to read it in the upcoming days

– scorpion9979
Mar 8 at 11:51





@E_net4 That looks like an awesome read! I will certainly make time to read it in the upcoming days

– scorpion9979
Mar 8 at 11:51




1




1





Thanks for posting a link that included your tests.

– jelford
Mar 9 at 18:36





Thanks for posting a link that included your tests.

– jelford
Mar 9 at 18:36












1 Answer
1






active

oldest

votes


















1














You can avoid re-traversing the list by keeping track of the last element you saw as you go (and then updating that at the end).



If you are naive about how you do that, you will run into trouble; your "previous" pointer retains ownership of the rest of the list and the borrow checker won't allow that. The trick is to break that link as you go - and to do that you can use the mem::replace function. Once you've done that, you have to put it back before you lose track of your previous node again.



Here's what that could look like in full (you'll have to forgive my liberal use of unwrap - I do think it makes things clearer):



pub fn pop(&mut self) -> Option<T>
where T : Copy,

use std::mem::replace;

let curr = replace(&mut self.head, None);

if curr.is_none() // list started off empty; nothing to pop
return None;


let mut curr = curr.unwrap(); // safe because of the check above

if let None = curr.next // popped the last element
return Some(curr.data);


let mut prev_next = &mut self.head;

while curr.next.is_some()
// Take ownership of the next element
let nnext = replace(&mut curr.next, None).unwrap();

// Update the previous element's "next" field
*prev_next = Some(curr);

// Progress to the next element
curr = nnext;

// Progress our pointer to the previous element's "next" field
prev_next = &mut prev_next.as_mut().unwrap().next;



return Some(curr.data);



As an aside, all this pointer shuffling simplifies a lot if you're willing to change the interface a little so that we return a "new" list each time (taking ownership in the pop function), or use a persistent datastructure, as they do in Learning Rust with entirely too many linked lists (already mentioned in a comment):



pub fn pop_replace(self) -> (Option<T>, Self) 
// freely mutate self and all the nodes



Which you would use like:



let elem, list = list.pop();





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55062035%2fwhat-would-be-a-better-way-to-implement-pop-in-my-single-linked-list-in-rust%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You can avoid re-traversing the list by keeping track of the last element you saw as you go (and then updating that at the end).



    If you are naive about how you do that, you will run into trouble; your "previous" pointer retains ownership of the rest of the list and the borrow checker won't allow that. The trick is to break that link as you go - and to do that you can use the mem::replace function. Once you've done that, you have to put it back before you lose track of your previous node again.



    Here's what that could look like in full (you'll have to forgive my liberal use of unwrap - I do think it makes things clearer):



    pub fn pop(&mut self) -> Option<T>
    where T : Copy,

    use std::mem::replace;

    let curr = replace(&mut self.head, None);

    if curr.is_none() // list started off empty; nothing to pop
    return None;


    let mut curr = curr.unwrap(); // safe because of the check above

    if let None = curr.next // popped the last element
    return Some(curr.data);


    let mut prev_next = &mut self.head;

    while curr.next.is_some()
    // Take ownership of the next element
    let nnext = replace(&mut curr.next, None).unwrap();

    // Update the previous element's "next" field
    *prev_next = Some(curr);

    // Progress to the next element
    curr = nnext;

    // Progress our pointer to the previous element's "next" field
    prev_next = &mut prev_next.as_mut().unwrap().next;



    return Some(curr.data);



    As an aside, all this pointer shuffling simplifies a lot if you're willing to change the interface a little so that we return a "new" list each time (taking ownership in the pop function), or use a persistent datastructure, as they do in Learning Rust with entirely too many linked lists (already mentioned in a comment):



    pub fn pop_replace(self) -> (Option<T>, Self) 
    // freely mutate self and all the nodes



    Which you would use like:



    let elem, list = list.pop();





    share|improve this answer





























      1














      You can avoid re-traversing the list by keeping track of the last element you saw as you go (and then updating that at the end).



      If you are naive about how you do that, you will run into trouble; your "previous" pointer retains ownership of the rest of the list and the borrow checker won't allow that. The trick is to break that link as you go - and to do that you can use the mem::replace function. Once you've done that, you have to put it back before you lose track of your previous node again.



      Here's what that could look like in full (you'll have to forgive my liberal use of unwrap - I do think it makes things clearer):



      pub fn pop(&mut self) -> Option<T>
      where T : Copy,

      use std::mem::replace;

      let curr = replace(&mut self.head, None);

      if curr.is_none() // list started off empty; nothing to pop
      return None;


      let mut curr = curr.unwrap(); // safe because of the check above

      if let None = curr.next // popped the last element
      return Some(curr.data);


      let mut prev_next = &mut self.head;

      while curr.next.is_some()
      // Take ownership of the next element
      let nnext = replace(&mut curr.next, None).unwrap();

      // Update the previous element's "next" field
      *prev_next = Some(curr);

      // Progress to the next element
      curr = nnext;

      // Progress our pointer to the previous element's "next" field
      prev_next = &mut prev_next.as_mut().unwrap().next;



      return Some(curr.data);



      As an aside, all this pointer shuffling simplifies a lot if you're willing to change the interface a little so that we return a "new" list each time (taking ownership in the pop function), or use a persistent datastructure, as they do in Learning Rust with entirely too many linked lists (already mentioned in a comment):



      pub fn pop_replace(self) -> (Option<T>, Self) 
      // freely mutate self and all the nodes



      Which you would use like:



      let elem, list = list.pop();





      share|improve this answer



























        1












        1








        1







        You can avoid re-traversing the list by keeping track of the last element you saw as you go (and then updating that at the end).



        If you are naive about how you do that, you will run into trouble; your "previous" pointer retains ownership of the rest of the list and the borrow checker won't allow that. The trick is to break that link as you go - and to do that you can use the mem::replace function. Once you've done that, you have to put it back before you lose track of your previous node again.



        Here's what that could look like in full (you'll have to forgive my liberal use of unwrap - I do think it makes things clearer):



        pub fn pop(&mut self) -> Option<T>
        where T : Copy,

        use std::mem::replace;

        let curr = replace(&mut self.head, None);

        if curr.is_none() // list started off empty; nothing to pop
        return None;


        let mut curr = curr.unwrap(); // safe because of the check above

        if let None = curr.next // popped the last element
        return Some(curr.data);


        let mut prev_next = &mut self.head;

        while curr.next.is_some()
        // Take ownership of the next element
        let nnext = replace(&mut curr.next, None).unwrap();

        // Update the previous element's "next" field
        *prev_next = Some(curr);

        // Progress to the next element
        curr = nnext;

        // Progress our pointer to the previous element's "next" field
        prev_next = &mut prev_next.as_mut().unwrap().next;



        return Some(curr.data);



        As an aside, all this pointer shuffling simplifies a lot if you're willing to change the interface a little so that we return a "new" list each time (taking ownership in the pop function), or use a persistent datastructure, as they do in Learning Rust with entirely too many linked lists (already mentioned in a comment):



        pub fn pop_replace(self) -> (Option<T>, Self) 
        // freely mutate self and all the nodes



        Which you would use like:



        let elem, list = list.pop();





        share|improve this answer















        You can avoid re-traversing the list by keeping track of the last element you saw as you go (and then updating that at the end).



        If you are naive about how you do that, you will run into trouble; your "previous" pointer retains ownership of the rest of the list and the borrow checker won't allow that. The trick is to break that link as you go - and to do that you can use the mem::replace function. Once you've done that, you have to put it back before you lose track of your previous node again.



        Here's what that could look like in full (you'll have to forgive my liberal use of unwrap - I do think it makes things clearer):



        pub fn pop(&mut self) -> Option<T>
        where T : Copy,

        use std::mem::replace;

        let curr = replace(&mut self.head, None);

        if curr.is_none() // list started off empty; nothing to pop
        return None;


        let mut curr = curr.unwrap(); // safe because of the check above

        if let None = curr.next // popped the last element
        return Some(curr.data);


        let mut prev_next = &mut self.head;

        while curr.next.is_some()
        // Take ownership of the next element
        let nnext = replace(&mut curr.next, None).unwrap();

        // Update the previous element's "next" field
        *prev_next = Some(curr);

        // Progress to the next element
        curr = nnext;

        // Progress our pointer to the previous element's "next" field
        prev_next = &mut prev_next.as_mut().unwrap().next;



        return Some(curr.data);



        As an aside, all this pointer shuffling simplifies a lot if you're willing to change the interface a little so that we return a "new" list each time (taking ownership in the pop function), or use a persistent datastructure, as they do in Learning Rust with entirely too many linked lists (already mentioned in a comment):



        pub fn pop_replace(self) -> (Option<T>, Self) 
        // freely mutate self and all the nodes



        Which you would use like:



        let elem, list = list.pop();






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 9 at 18:38

























        answered Mar 9 at 18:28









        jelfordjelford

        2,25111431




        2,25111431





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55062035%2fwhat-would-be-a-better-way-to-implement-pop-in-my-single-linked-list-in-rust%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

            Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

            Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved