使用带有 seq::index::sample 的排序时出现 Rust 错误
Rust error when using sort with seq::index::sample
所以我试图生成一个包含 5 个数字的样本:
use rand::{thread_rng, seq};
use std::time::SystemTime;
fn main(){
let mut rng = thread_rng();
let mut sample = seq::index::sample(&mut rng, 50, 5);
}
但是当我尝试使用以下方式对其进行排序时:
sample.sort();
它给我以下错误:
error[E0599]: no method named sort
found for type
rand::seq::index::IndexVec
in the current scope -->
src/main.rs:12:16
如何对随机数字样本进行排序?
rand::seq::index::sample
returns an rand::seq::index::IndexVec
而不是常规的 Vec
。这个类型貌似主要是为了迭代,方法不多。特别是,它没有 sort
方法。
您可以使用 into_vec
获得正常的 Vec
,这是 sort
可用的。
所以我试图生成一个包含 5 个数字的样本:
use rand::{thread_rng, seq};
use std::time::SystemTime;
fn main(){
let mut rng = thread_rng();
let mut sample = seq::index::sample(&mut rng, 50, 5);
}
但是当我尝试使用以下方式对其进行排序时:
sample.sort();
它给我以下错误:
error[E0599]: no method named
sort
found for typerand::seq::index::IndexVec
in the current scope --> src/main.rs:12:16
如何对随机数字样本进行排序?
rand::seq::index::sample
returns an rand::seq::index::IndexVec
而不是常规的 Vec
。这个类型貌似主要是为了迭代,方法不多。特别是,它没有 sort
方法。
您可以使用 into_vec
获得正常的 Vec
,这是 sort
可用的。