gpt4 book ai didi

rust - 代码无需借用即可工作,但我无法通过借用使其工作

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

我有一些代码并且它可以工作,但是 AFAIK 借用是专门为了避免像我在 split_text 方法中那样手动将相同的对象传入和传出方法。

fn main() {
println!("Hello, world!");
let mut test = String::from("12345");
let mut obj1 = Object {
text: test.as_mut_str(),
next: None,
};
for i in 0..5 {
obj1 = obj1.split_text(4 - i);
if let Some(obj) = obj1.next.as_ref() {
println!("{}", obj.text);
}
}
}

struct Object<'a> {
text: &'a mut str,
next: Option<Box<Object<'a>>>,
}

impl<'a> Object<'a> {
fn split_text(mut self, count: usize) -> Self {
let tmp = self.text;
let (part1, part2) = tmp.split_at_mut(count);
self.text = part1;
let obj2 = Object {
text: part2,
next: None,
};
self.next = Some(Box::new(obj2));
self
}
}

( Playground )

但我不知道如何通过借阅检查器

impl<'a> Object<'a> {
fn split_text(&'a mut self, count: usize) {
let tmp = &mut self.text;
let (part1, part2) = tmp.split_at_mut(count);
self.text = part1;
let obj2 = Object {
text: part2,
next: None,
};
self.next = Some(Box::new(obj2));
}
}

导致错误

error[E0499]: cannot borrow `obj1` as mutable more than once at a time
--> src/main.rs:11:9
|
11 | obj1.split_text(4 - i);
| ^^^^ mutable borrow starts here in previous iteration of loop

error[E0502]: cannot borrow `obj1.next` as immutable because it is also borrowed as mutable
--> src/main.rs:12:28
|
11 | obj1.split_text(4 - i);
| ---- mutable borrow occurs here
12 | if let Some(obj) = obj1.next.as_ref() {
| ^^^^^^^^^
| |
| immutable borrow occurs here
| mutable borrow later used here

error[E0506]: cannot assign to `self.text` because it is borrowed
--> src/main.rs:27:9
|
23 | impl <'a> Object<'a> {
| -- lifetime `'a` defined here
24 | fn split_text(&'a mut self, count:usize) {
25 | let tmp = &mut self.text;
| -------------- borrow of `self.text` occurs here
26 | let (part1, part2) = tmp.split_at_mut(count);
| ----------------------- argument requires that `self.text` is borrowed for `'a`
27 | self.text = part1;
| ^^^^^^^^^^^^^^^^^ assignment to borrowed `self.text` occurs here

( Playground )

有没有办法让这段代码起作用?

最佳答案

问题是您借用了函数 split_text 中的可变对象 obj1 直到 'a 生命周期结束主要函数。

fn main() {
println!("Hello, world!");
let mut test = String::from("12345");
let mut obj1 = Object { // 'a start
text: test.as_mut_str(),
next: None,
};
for i in 0..5 {
obj1 = obj1.split_text(4 - i); // borrow for 'a lifetime,
// Won't work in next iteration
if let Some(obj) = obj1.next.as_ref() { // Won't work
println!("{}", obj.text);
}
}
} // 'a end

你只想为 split_text 函数可变地借用它,即为不同的(更小的)生命周期 - 你可以省略或指定不同的生命周期 - 比如 'b .

struct Object<'a> {
text: &'a str, // no need of mutable slice here
next: Option<Box<Object<'a>>>,
}

impl<'a> Object<'a> {
fn split_text(&mut self, count: usize) { // No lifetime required here
let (part1, part2) = self.text.split_at(count); // no need of temp var and mutable split here
self.text = part1;
let obj2 = Object {
text: part2,
next: None,
};
self.next = Some(Box::new(obj2));
}
}

明确不同的生命周期版本(只是为了完整性):

impl<'a> Object<'a> {
fn split_text<'b>(&'b mut self, count: usize) {
let tmp = &mut self.text;
let (part1, part2) = tmp.split_at(count);
self.text = part1;
let obj2 = Object {
text: part2,
next: None,
};
self.next = Some(Box::new(obj2));
}
}

此外,仅在需要时才使某些内容可变。我更改了可变切片并将其拆分为正常。

关于rust - 代码无需借用即可工作,但我无法通过借用使其工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55607123/

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