[英]How can I swap items in a vector, slice, or array in Rust?
My code looks like this: 我的代码看起来像这样:
fn swap<T>(mut collection: Vec<T>, a: usize, b: usize) {
let temp = collection[a];
collection[a] = collection[b];
collection[b] = temp;
}
Rust is pretty sure I'm not allowed to "move out of dereference" or "move out of indexed content", whatever that is. Rust很确定我不允许“脱离引用”或“移出索引内容”,不管是什么。 How do I convince Rust that this is possible? 我如何说服Rust这是可能的?
There is a swap
method defined for &mut [T]
. 为&mut [T]
定义了一个swap
方法 。 Since a Vec<T>
can be mutably dereferenced as a &mut [T]
, this method can be called directly: 由于Vec<T>
可以作为&mut [T]
被可释放地解引用 ,因此可以直接调用此方法:
fn main() {
let mut numbers = vec![1, 2, 3];
println!("before = {:?}", numbers);
numbers.swap(0, 2);
println!("after = {:?}", numbers);
}
To implement this yourself, you have to write some unsafe code. 要自己实现,必须编写一些不安全的代码。 Vec::swap
is implemented like this: Vec::swap
实现如下:
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);
}
}
It takes two raw pointers from the vector and uses ptr::swap
to swap them safely. 它从向量中获取两个原始指针,并使用ptr::swap
安全地交换它们。
There is also a mem::swap(&mut T, &mut T)
when you need to swap two distinct variables. 当你需要交换两个不同的变量时mem::swap(&mut T, &mut T)
还有一个mem::swap(&mut T, &mut T)
。 That cannot be used here because Rust won't allow taking two mutable borrows from the same vector. 这不能在这里使用,因为Rust不允许从同一个向量中取两个可变的借位。
As mentioned by @gsingh2011, the accepted answer is no longer good approach. 正如@ gsingh2011所提到的,接受的答案不再是好方法。 With current Rust this code works fine: 使用当前的Rust,这段代码运行良好:
fn main() {
let mut numbers = vec![1, 2, 3];
println!("before = {:?}", numbers);
numbers.swap(0, 2);
println!("after = {:?}", numbers);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.