What would be a better way to implement .pop() in my single linked list in Rust?Two 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?
Short story about space worker geeks who zone out by 'listening' to radiation from stars
Do all network devices need to make routing decisions, regardless of communication across networks or within a network?
Escape a backup date in a file name
How does it work when somebody invests in my business?
How does Loki do this?
Trouble understanding the speech of overseas colleagues
Failed to fetch jessie backports repository
Is expanding the research of a group into machine learning as a PhD student risky?
How do we know the LHC results are robust?
CREATE opcode: what does it really do?
I'm in charge of equipment buying but no one's ever happy with what I choose. How to fix this?
Sequence of Tenses: Translating the subjunctive
Detecting if an element is found inside a container
How can a function with a hole (removable discontinuity) equal a function with no hole?
Do sorcerers' subtle spells require a skill check to be unseen?
How long to clear the 'suck zone' of a turbofan after start is initiated?
How does buying out courses with grant money work?
How to pronounce the slash sign
Why didn't Theresa May consult with Parliament before negotiating a deal with the EU?
What does the word "Atten" mean?
India just shot down a satellite from the ground. At what altitude range is the resulting debris field?
Would a high gravity rocky planet be guaranteed to have an atmosphere?
Unreliable Magic - Is it worth it?
Tiptoe or tiphoof? Adjusting words to better fit fantasy races
What would be a better way to implement .pop() in my single linked list in Rust?
Two 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?
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
add a comment |
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
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
add a comment |
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
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
linked-list rust singly-linked-list
edited Mar 8 at 12:02
Wesley Wiser
6,73143351
6,73143351
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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();
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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();
add a comment |
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();
add a comment |
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();
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();
edited Mar 9 at 18:38
answered Mar 9 at 18:28
jelfordjelford
2,24611431
2,24611431
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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