借用结构,向量和函数的多个可变变量

问题描述

我正在尝试强化Rust中的借用概念。我知道您不能多次借用可变引用。在这里,我尝试了一些测试,发现了一些我无法完全理解的特殊情况。

fn main() {
    pub fn test() {
        println!("{}","testing success".to_string())
    };

    pub struct Test<'a> {
        pub func: &'a mut dyn FnMut() -> (),pub vector: &'a mut Vec<String>,};

    impl<'a> Test<'a> {
        pub fn repr(&self) -> String {
            format!("{}","test".to_string())
        }
        pub fn make_clone(&'a mut self) -> Self {
            Test {
                func: self.func,vector: self.vector,}
        }
    }

    let mut test_vec = vec!["jkli".to_string(),"sadf".to_string()];
    let mut test_var = Test {
        func: &mut test,vector: &mut test_vec,};

    let mut another_test_var = Test { // <= 1st "copy" of test_var
        func: &mut test,// <= #1 This line DOES NOT throw a mutable borrow error.
        vector: &mut test_vec,// <= #2 This line throws a mutable borrow error as expected.
    };

    let mut new_test_var = test_var.make_clone(); // <= #3 This line also DOES NOT throw an error
                                                  // even though it should behave like the 1st "copy

    let mut last_test_var = new_test_var.make_clone();

    test_var.repr();         // <= #4 This gives an error
    another_test_var.repr();
    new_test_var.repr();     // <= #5 This gives an error as well
    last_test_var.repr();
}

Windows 10,rustc 1.47

  • 我希望#4和#5会引发多个可变的借用错误,因为两个结构都用于创建自己的“副本”(至少我认为我理解这一点)。
  • #2引发错误,因为我试图第二次可变地引用向量,所以这也应该引发错误
  • 为什么#1不会给出错误,因为我像矢量一样借用了每个结构的相同函数
  • 为什么#3不会引发错误?它应该与第一个副本相同

错误

  --> src\main.rs:37:9
   |
37 |     let mut last_test_var = new_test_var.make_clone();
   |         ----^^^^^^^^^^^^^
   |         |
   |         help: remove this `mut`

error[E0499]: cannot borrow `test_vec` as mutable more than once at a time
  --> src\main.rs:31:17
   |
26 |         vector: &mut test_vec,|                 ------------- first mutable borrow occurs here
...
31 |         vector: &mut test_vec,// <= #2 This line throws a mutable borrow error as expected.
   |                 ^^^^^^^^^^^^^ second mutable borrow occurs here
...
34 |     let mut new_test_var = test_var.make_clone(); // <= #3 This line also DOES NOT throw an error
   |                            -------- first borrow later used here

error[E0502]: cannot borrow `test_var` as immutable because it is also borrowed as mutable
  --> src\main.rs:39:5
   |
34 |     let mut new_test_var = test_var.make_clone(); // <= #3 This line also DOES NOT throw an error
   |                            -------- mutable borrow occurs here
...
39 |     test_var.repr();         // <= #4 This gives an error
   |     ^^^^^^^^ immutable borrow occurs here
40 |     another_test_var.repr();
41 |     new_test_var.repr();     // <= #5 This gives an error as well
   |     ------------ mutable borrow later used here

error[E0502]: cannot borrow `new_test_var` as immutable because it is also borrowed as mutable
  --> src\main.rs:41:5
   |
37 |     let mut last_test_var = new_test_var.make_clone();
   |                             ------------ mutable borrow occurs here
...
41 |     new_test_var.repr();     // <= #5 This gives an error as well
   |     ^^^^^^^^^^^^ immutable borrow occurs here
42 |     last_test_var.repr();
   |     ------------- mutable borrow later used here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)