gpt4 book ai didi

data-structures - 如何在不需要分配给新变量的情况下为链表实现前置?

转载 作者:行者123 更新时间:2023-11-29 08:31:43 25 4
gpt4 key购买 nike

有些东西告诉我如何实现链表:

enum List {
Cons(u32, Box<List>),
Nil,
}

impl List {
fn prepend(self, elem: u32) -> List {
Cons(elem, Box::new(self))
}
}

当我想使用prepend时,我需要做以下事情:

list = list.prepend(1);

但是,我想创建一个不需要在每次 prepend 返回时都创建新变量的函数。我只想使用 prepend 更改 list 变量本身:

list.prepend(1);

这是我提出的一种实现方式,但它并不正确:

fn my_prepend(&mut self, elem: u32) {
*self = Cons(elem, Box::new(*self));
}

错误是:

error[E0507]: cannot move out of borrowed content

最佳答案

List::prepend 必须 move self 因为这确实是正在发生的事情。链表的新头是一个新对象,旧头移到堆上,使旧变量无效。

my_prepend 中,您有一个对 self 的可变引用,但随后您 move 了它的值,使 self 引用变得无效。尽管它只是暂时无效,但这就是消息 “cannot move out of borrowed content” 所提示的。

解决这个问题的一种方法是将 self 移出到一个变量中,同时将其替换为 Nil,这样 self 引用永远不会无效。你可以用 mem::replace 来做到这一点:

use std::mem;

fn my_prepend(&mut self, elem: u32) {
// Move the value of self into head, and leave self as Nil so it isn't invalid
let head = mem::replace(self, List::Nil);
// Reassign to self with the prepended value
*self = head.prepend(elem);
}

关于data-structures - 如何在不需要分配给新变量的情况下为链表实现前置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56087004/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com