如何在Rust中将元素插入链表的前面?

huangapple go评论77阅读模式
英文:

How to push front in linked list Rust?

问题

我在Rust中尝试实现链表。我的代码:

  1. #[derive(Eq, PartialEq, Clone, Debug)]
  2. struct ListNode {
  3. val: i32,
  4. next: Option<Box<ListNode>>,
  5. }
  6. impl ListNode {
  7. fn new(val: i32) -> Self {
  8. println!("Call new {}", val);
  9. return ListNode {
  10. val: val,
  11. next: None,
  12. };
  13. }
  14. fn push_front(&mut self, new_head: &mut ListNode) {
  15. let old_next = std::mem::replace(&mut new_head.next, None);
  16. self.next = Some(Box::new(std::mem::replace(new_head, ListNode::new(0))));
  17. new_head.next = old_next;
  18. }
  19. }
  20. fn main() {
  21. let mut node1 = ListNode::new(1);
  22. let mut node2 = ListNode::new(2);
  23. node2.push_front(&mut node1);
  24. }

我在以下代码中有一个错误:

  1. error[E0507]: cannot move out of a mutable reference: fn push_front(self, &mut new_head: &mut ListNode)

如何修复这个错误?我认为Copy trait的实现不是一个好主意。

希望这能帮助你解决问题。如果你有任何其他问题,请随时提出。

英文:

I am trying to implement linked list in Rust. My code:

  1. #[derive(Eq, PartialEq, Clone, Debug)]
  2. struct ListNode {
  3. val: i32,
  4. next: Option&lt;Box&lt;ListNode&gt;&gt;
  5. }
  6. impl ListNode {
  7. fn new(val: i32) -&gt; Self {
  8. println!(&quot;Call new {}&quot;, val);
  9. return ListNode {
  10. val: val,
  11. next: None
  12. };
  13. }
  14. fn push_front(self, &amp;mut new_head: &amp;mut ListNode) -&gt; ListNode {
  15. new_head.next = Some(Box::new(self));
  16. return new_head;
  17. }
  18. }
  19. fn main() {
  20. let mut node1 = ListNode::new(1);
  21. let mut node2 = ListNode::new(2);
  22. node2 = node1.push_front(&amp;mut node2);
  23. }

I have an error in
> error[E0507]: cannot move out of a mutable reference: fn push_front(self, &mut new_head: &mut ListNode) -> ListNode {

How can I fix the error? I think that Copytrait implementation is not a good idea

答案1

得分: 4

你直接从 push_front 返回 ListNode,这意味着返回值将被移出。但是当你只有一个引用时,你不能移动数据。

如果你确定想让调用者拥有所有权,你可以允许将 new_head 参数也移动到函数中。

  1. impl ListNode {
  2. // ...
  3. fn push_front(self, mut new_head: ListNode) -> ListNode {
  4. new_head.next = Some(Box::new(self));
  5. new_head
  6. }
  7. }
  8. fn main() {
  9. let node1 = ListNode::new(1);
  10. let mut node2 = ListNode::new(2);
  11. node2 = node1.push_front(node2);
  12. println!("{node2:?}");
  13. }
英文:

You're directly returning ListNode from push_front, which means that the return value will be moved out. But you cannot move the data when you only have a reference.

If you are sure that you want the caller to take ownership, you can allow the new_head argument to be moved into the function too.

  1. impl ListNode {
  2. // ...
  3. fn push_front(self, mut new_head: ListNode) -&gt; ListNode {
  4. new_head.next = Some(Box::new(self));
  5. new_head
  6. }
  7. }
  8. fn main() {
  9. let node1 = ListNode::new(1);
  10. let mut node2 = ListNode::new(2);
  11. node2 = node1.push_front(node2);
  12. println!(&quot;{node2:?}&quot;);
  13. }

huangapple
  • 本文由 发表于 2023年7月14日 00:38:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76681609.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定