不可以直接用std::mem::swap
,因为这个函数需要拿两个可变引用,但是不可以同时拿两个这个数组的可变引用。
所以要么手写:
let tmp = a[i]; |
要么用Vec::swap
:
a.swap(i, j); |
其内部实现:
fn swap(&mut self, a: usize, b: usize) { |
不可以直接用std::mem::swap
,因为这个函数需要拿两个可变引用,但是不可以同时拿两个这个数组的可变引用。
所以要么手写:
let tmp = a[i]; |
要么用Vec::swap
:
a.swap(i, j); |
其内部实现:
fn swap(&mut self, a: usize, b: usize) { |
Except as otherwise noted, this blog is licensed under CC BY-SA 4.0 License.
©2021-
searchstar
|
pv
|
uv
Theme Tree
by Wu Jun
Powered by Hexo