rust交换数组中的两个元素

不可以直接用std::mem::swap,因为这个函数需要拿两个可变引用,但是不可以同时拿两个这个数组的可变引用。

所以要么手写:

let tmp = a[i];
a[i] = a[j];
a[j] = tmp;

要么用Vec::swap

a.swap(i, j);

其内部实现:

fn swap(&mut self, a: usize, b: usize) {
    unsafe {
        // Can't take two mutable loans from one vector, so instead just cast
        // them to their raw pointers to do the swap
        let pa: *mut T = &mut self[a];
        let pb: *mut T = &mut self[b];
        ptr::swap(pa, pb);
    }
}

原文:https://stackoverflow.com/questions/25531963/how-can-i-swap-items-in-a-vector-slice-or-array-in-rust/25532111

posted @ 2024-09-28 13:45  寻找繁星  阅读(16)  评论(0编辑  收藏  举报