cannot assign to `self.b` because it is borrowed `self.b` is assigned to here but it was already borrowed

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

cannot assign to `self.b` because it is borrowed `self.b` is assigned to here but it was already borrowed

问题

这是我的代码的一个最小子集。我的Rust编译器报告说"无法分配给self.b,因为它已被借用。self.b在此处分配,但它已经被借用"。

struct Test {
    a: String,
    b: String,
}

impl Test {
    fn borrow_func(&self) -> &String {
        &self.a
    }

    fn test_func(&mut self) -> usize {
        let a = self.borrow_func();
        self.b = "".to_string();
        a.len()
    }
}

当我内联函数borrow_func时,错误被省略了。然而,内联的代码缺乏可读性。我如何在保留可读性的同时修复这个错误?

这是内联的代码:

struct Test {
    a: String,
    b: String,
}

impl Test {
    fn test_func(&mut self) -> usize {
        let a = &self.a;
        self.b = "".to_string();
        a.len()
    }
}
英文:

Here is a minimal subset of my code. My Rust compiler reports "cannot assign to self.b because it is borrowed. self.b is assigned to here but it was already borrowed"

struct Test {
    a: String,
    b: String,
}

impl Test {
    fn borrow_func(&self) -> &String {
        &self.a
    }

    fn test_func(&mut self) -> usize {
        let a = self.borrow_func();
        self.b = "".to_string();
        a.len()
    }
}

When I inline the function borrow_func, the error is omitted. However, the inlined code lacks readability. How can I fix the bug while preserving the readability?

Here is the inlined code:

struct Test {
    a: String,
    b: String,
}

impl Test {
    fn test_func(&mut self) -> usize {
        let a = &self.a;
        self.b = "".to_string();
        a.len()
    }
}

答案1

得分: 3

解决方案只是重新排列操作,你执行操作时有一个不可变引用和可变引用同时处于活动状态,这就是为什么借用检查器会报告你已经借用了一个变量的错误。

我建议你学习 Rust 的生命周期,并尝试理解借用和所有权。

https://doc.rust-lang.org/book/ch04-00-understanding-ownership.html

struct Test {
    a: String,
    b: String,
}

impl Test {
    fn borrow_func(&self) -> &String {
        &self.a
    }

    fn test_func(&mut self) -> usize {
        self.b = "".to_string();
        let a = self.borrow_func();
        a.len()
    }
}

fn main() {
    let mut t: Test = Test {
        a: String::from("hola"),
        b: String::from("hello"),
    };

    println!("{}", t.test_func());
}
英文:

The solution is just reorder the operations, The way you do the operations you have an inmmutable reference active and mutable reference active, so that's why the borrow checker throws the error about you have borrowed a variable.

I would recommend to study the rust lifetimes and trying to understand borrowing and ownership.

https://doc.rust-lang.org/book/ch04-00-understanding-ownership.html


struct Test {
    a: String,
    b: String,
}

impl Test {
    fn borrow_func(&self) -> &String {
        &self.a
    }

    fn test_func(&mut self) -> usize {
        self.b = "".to_string();
        let a = self.borrow_func();
        a.len()
    }
}

fn main() {
    let mut t: Test = Test {
        a: String::from("hola"),
        b: String::from("hello"),
    };

    println!("{}", t.test_func());
}

答案2

得分: -1

我发现这段代码有效

#[derive(Clone)]
struct Test {
    a: String,
    b: String,
}

impl Test {
    fn borrow_func(&self) -> &String {
        &self.a
    }

    fn test_func(&mut self) -> usize {
        let s = self.clone();
        let a = s.borrow_func();
        self.b = "".to_string();
        a.len()
    }
}

发生的情况是当运行 self.borrow_func() 时,它传递的是 self 而不是 &self,因为 self 已经是一个引用。为了解决这个问题,我们应该改成

self.clone().borrow_func();

但这会导致一个错误,说它是一个临时值,所以为了解决这个问题,我们可以这样做

let s = self.clone();
s.borrow_func();
英文:

I have found that this code works

#[derive(Clone)]
struct Test {
    a: String,
    b: String,
}

impl Test {
    fn borrow_func(&self) -> &String {
        &self.a
    }

    fn test_func(&mut self) -> usize {
        let s = self.clone();
        let a = s.borrow_func();
        self.b = "".to_string();
        a.len()
    }
}

What is happening is that when self.borrow_func() is run it's passing self not &self because self is already a reference. To fix this we would instead do

self.clone().borrow_func();

But that gives us an error that it is a temporary value so to fix this we do

let s = self.clone();
s.borrow_func();

huangapple
  • 本文由 发表于 2023年7月17日 13:13:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701640.html
匿名

发表评论

匿名网友

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

确定