考虑一下我有一个make_numbers
应该创建一串随机数的函数,但是我想在运行时(用户输入)决定应该使用哪种随机数生成器的情况。为了使它更加困难,让我们假设该make_numbers
函数对要生成的数字类型是通用的。
我用伪代码写了我想要实现的东西,我明白为什么这不起作用。但是,我不知道 Rust 中的惯用方式会是什么样子来实现这一点?
我天真的想法是:
- 使用
Box<Rng>
,但这不起作用,因为Rng
它具有通用功能。 StdRng
在and上使用枚举XorShiftRng
,但我真的想不出一个好的方法来写这个。
你能给我一些关于这个特定问题的好的解决方案的提示吗?
注意:这个问题不是关于具有不同类型的不同匹配臂(解决方案可以是Box
或枚举,如上所述) - 而是如何在这种情况下应用这些解决方案。
extern crate rand;
use rand::{Rng, SeedableRng, StdRng};
use rand::prng::XorShiftRng;
use std::string::String;
use rand::distributions::{Distribution, Standard};
use std::fmt::Display;
// Generic function that should work with any type of random number generator
fn make_numbers<T, R: Rng>(rng: &mut R) -> String
where T: Display, Standard: Distribution<T>
{
let mut s = String::new();
for _i in 0..10 {
s.push_str(format!("_{}", rng.gen::<T>()).as_str());
}
s
}
fn main() {
let use_std = true; // -> assume that this will be determined at runtime (e.g. user input)
// Pseudo code, will not work.
let mut rng = match use_std {
true => StdRng::from_seed(b"thisisadummyseedthisisadummyseed".to_owned()),
false => XorShiftRng::from_seed(b"thisisadummyseed".to_owned())
};
let s = make_numbers::<u8>(&mut rng);
// ... do some complex stuff with s ...
print!("{}", s)
}
error[E0308]: match arms have incompatible types
--> src/main.rs:24:19
|
24 | let mut rng = match use_std {
| ___________________^
25 | | true => StdRng::from_seed(b"thisisadummyseedthisisadummyseed".to_owned()),
26 | | false => XorShiftRng::from_seed(b"thisisadummyseed".to_owned())
| | ------------------------------------------------------ match arm with an incompatible type
27 | | };
| |_____^ expected struct `rand::StdRng`, found struct `rand::XorShiftRng`
|
= note: expected type `rand::StdRng`
found type `rand::XorShiftRng`