搬家后,这里借来的 Value 问题是什么? [等候接听]

loading...


-5

有我的代码,我不知道是什么问题,所以, Ant 非常感谢 . 并且有所有代码 . 当您运行此代码时,将在移动错误消息后抛出此处借用的值 .

pub fn median_of_two_sorted_arrays(vec1: Vec<i32>, vec2: Vec<i32>) -> i32 {
    let _mlen: i32 = (vec1.len() + vec2.len()) as i32;

    let mut index1 = vec1.len() / 2 - 1;
    let mut index2 = vec2.len() / 2 - 1;

    let get_min_index = |v1: Vec<i32>, i1, v2: Vec<i32>, i2| {
        let mut i = i1;
        while v1[i] > v2[i2] && i > 0 {
            i -= 1;
        }
        return i;
    };

    if vec1[index1] > vec2[index2] {
        index1 = get_min_index(vec1, index1, vec2, index2);
    } else {
        index2 = get_min_index(vec2, index2, vec1, index1);
    }

    if _mlen % 2 == 1 {
        let _index = (_mlen / 2) as usize;

        while _index > index1 + index2 + 1 {
            if vec1[index1] > vec2[index2] {
                index2 += 1;
            } else {
                index1 += 1;
            }
        }

        println!("{},{}", index1, index2);
    } else {
        let _index = _mlen / 2 - 1;
    }

    return 0;
}

fn main() {
    let vec1 = vec![9, 13, 17, 19, 24, 28, 38];
    let vec2 = vec![2, 7, 9, 13];

    let results = median_of_two_sorted_arrays(vec1, vec2);
    println!("{}", results);
}

loading...

1回答

  • 1

    如果您只需要知道问题,那么只需查看编译器错误:

    error[E0382]: borrow of moved value: `vec1`                                                                                                              
      --> src/main.rs:25:16                                                                                                                                  
       |                                                                                                                                                     
    16 |         index1 = get_min_index(vec1, index1, vec2, index2);                                                                                         
       |                                ---- value moved here                                                                                                
    17 |     } else {                                                                                                                                        
    18 |         index2 = get_min_index(vec2, index2, vec1, index1);                                                                                         
       |                                              ---- value moved here                                                                                  
    ...                                                                                                                                                      
    25 |             if vec1[index1] > vec2[index2] {                                                                                                        
       |                ^^^^ value borrowed here after move                                                                                                  
       |                                                                                                                                                     
       = note: move occurs because `vec1` has type `std::vec::Vec<i32>`, which does not implement the `Copy` trait
    

    在第16行和第18行,移动了 vec1 的值,因此 vec1 不再有效 . 但是你试着在第25行借用它,这是不允许的 .

评论

暂时没有评论!