我有以下代码:
extern crate rand;
use rand::{thread_rng, Rng};
fn main() {
let mut vec: Vec<u32> = (0..10).collect();
let mut slice: &[u32] = vec.as_mut_slice();
thread_rng().shuffle(slice);
}
并得到以下错误:
error[E0308]: mismatched types
--> src/main.rs:9:26
|
9 | thread_rng().shuffle(slice);
| ^^^^^ types differ in mutability
|
= note: expected type `&mut [_]`
found type `&[u32]`
我想我知道向量和切片的内容是不可变的,这导致了这里的错误,但我不确定.
as_mut_slice
的签名是pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T]
,所以切片应该是可变的,但不知为什么它不是.
我知道一定有一个简单的解决办法,但我尽了最大努力,还是没能让它起作用.