如何从范围创建Vec并将其洗牌? [英] How do I create a Vec from a range and shuffle it?

查看:286
本文介绍了如何从范围创建Vec并将其洗牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

  extern crate rand; 

使用rand :: {thread_rng,Rng};

fn main(){
let mut vec:Vec< u32> =(0..10).collect();
让mut slice:& [u32] = vec.as_mut_slice();

thread_rng()。shuffle(slice);
}

并收到以下错误:

 错误[E0308]:不匹配的类型
- > src / main.rs:9:26
|
9 | 。thread_rng()洗牌(片);
| ^^^^^类型的可变性不同
|
=注意:预期类型`& mut [_]`
找到类型`& [u32]`

我想我理解向量和切片的内容是不可变的,这会导致错误,但我不确定。



as_mut_slice 的签名是 pub fn as_mut_slice<'a>(&'a mut self) - > &'a mut [T] ,所以切片应该是可变的,但它不知道。



我知道必须是一个简单的解决方案,但我尽我所能,无法让它工作。

解决方案

你非常接近。这应该有效:

  extern crate rand; 

使用rand :: {thread_rng,Rng};

fn main(){
let mut vec:Vec< u32> =(0..10).collect();
let slice:& mut [u32] =& mut vec;

thread_rng()。shuffle(slice);
}

& mut [T] 可隐式强制转换为& [T] ,并使用<$ c $注释 slice 变量c>& [u32] ,因此切片变为不可变:& mut [u32] 被强制转换为& [U32] 。变量上的 mut 与此处不相关,因为切片只是借用其他人拥有的数据,因此它们没有继承的可变性 - 它们的可变性在它们的类型中编码。 / p>

实际上,您根本不需要在 slice 上添加注释。这也有效:

  extern crate rand; 

使用rand :: {thread_rng,Rng};

fn main(){
let mut vec:Vec< u32> =(0..10).collect();
let slice = vec.as_mut_slice();

thread_rng()。shuffle(slice);
}

您甚至不需要中间变量:

  extern crate rand; 

使用rand :: {thread_rng,Rng};

fn main(){
let mut vec:Vec< u32> =(0..10).collect();
thread_rng()。shuffle(& mut vec);
}

你应该阅读 Rust编程语言 ,因为它解释了所有权和借用的概念以及它们如何与之交互可变性。


I have the following code:

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);
}

and get the following error:

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]`

I think I understand that the content of vectors and slices is immutable and that causes the error here, but I'm unsure.

The signature of as_mut_slice is pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T], so the slice should be mutable, but it somehow isn't.

I know that there must be an easy fix, but I tried my best and couldn't get it to work.

解决方案

You're very close. This should work:

extern crate rand;

use rand::{thread_rng, Rng};

fn main() {
    let mut vec: Vec<u32> = (0..10).collect();
    let slice: &mut [u32] = &mut vec;

    thread_rng().shuffle(slice);
}

&mut [T] is implicitly coercible to &[T], and you annotated the slice variable with &[u32], so the slice became immutable: &mut [u32] was coerced to &[u32]. mut on the variable is not relevant here because slices are just borrows into data owned by someone else, so they do not have inherited mutability - their mutability is encoded in their types.

In fact, you don't need an annotation on slice at all. This works as well:

extern crate rand;

use rand::{thread_rng, Rng};

fn main() {
    let mut vec: Vec<u32> = (0..10).collect();
    let slice = vec.as_mut_slice();

    thread_rng().shuffle(slice);
}

You don't even need the intermediate variable:

extern crate rand;

use rand::{thread_rng, Rng};

fn main() {
    let mut vec: Vec<u32> = (0..10).collect();
    thread_rng().shuffle(&mut vec);
}

You should read The Rust Programming Language as it explains the concepts of ownership and borrowing and how they interact with mutability.

这篇关于如何从范围创建Vec并将其洗牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆