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

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

问题描述

我有以下代码:

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

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

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

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

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.

推荐答案

兰德 v0.6.0

现在不推荐使用 Rng::shuffle 方法;应该使用 rand::seq::SliceRandom 特征.它在所有切片上提供 shuffle() 方法,该方法接受 Rng 实例:

Rand v0.6.0

The Rng::shuffle method is now deprecated; rand::seq::SliceRandom trait should be used. It provides the shuffle() method on all slices, which accepts an Rng instance:

// Rust edition 2018 no longer needs extern crate

use rand::thread_rng;
use rand::seq::SliceRandom;

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

看到它在游乐场.

你很亲密.这应该有效:

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] 隐式强制转换为 &[T],并且您使用 注释了 slice 变量&[u32],因此切片变得不可变:&mut [u32] 被强制转换为 &[u32].变量上的 mut 在这里不相关,因为切片只是借用到其他人拥有的数据中,因此它们没有继承的可变性 - 它们的可变性被编码在它们的类型中.

&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.

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

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

您应该阅读 Rust 编程语言 因为它解释了所有权和借用的概念以及它们如何与可变性相互作用.

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天全站免登陆