作为学习 Rust 的借口,我正在研究遗传算法的代码,以及稍后的遗传编程。
我声明了一个变异操作的特征:
pub trait Mutator<IndvidualType> {
fn mutate(&self, individual: &IndvidualType) -> IndvidualType;
}
为每个 IndividualType
实现特征很容易,但我想要更通用的东西,一个对每个列表(向量)类型基因组都很常见的特征,比如:
pub trait HasVectorGenome<IndividualType, BaseType> {
fn new_from_vec(genome: Vec<BaseType>) -> IndvidualType;
fn get_vec(&self) -> Vec<BaseType>;
}
我想要一个通用的更改器(mutator),它能够变异每个 HasVectorGenome
的 BaseType
实现 Rand
(为了能够生成一个新的随机值)。像这样的东西:
struct GeneralMutator;
impl<B, T> Mutator<T> for GeneralMutator
where T: HasVectorGenome<T, B>,
B: Rand
{
fn mutate(&self, individual: &T) -> T {
let genome: Vec<B> = individual.get_vec();
genome[0] = rand::random::<B>();
T::new_from_vec(genome)
}
}
我遇到了错误类型参数 `B` 不受 impl 特征、自身类型或谓词的约束
,我无法编译。我不知道如何正确表达这一点。
最佳答案
我已经放了这段代码的完整工作版本 on the playground (除了我删掉了随机部分)。
首先,我从 HasVectorGenome
中删除了 IndividualType
参数。这只是实现特征的类型,并且您对特征的定义与此不一致(new_from_vec
返回 IndividualType
但 get_vec
消耗 self
)。
其次,我将 BaseType
设为关联类型,这意味着任何单独的类型都有一个唯一的基类型。这在技术上是一个限制,但在大多数情况下,您不需要灵 active ,它使类型更简单(实际上是消除您看到的错误所需的主要更改)。所以现在的特征是:
pub trait HasVectorGenome {
type BaseType;
fn new_from_vec(genome: Vec<Self::BaseType>) -> Self;
fn get_vec(&self) -> Vec<Self::BaseType>;
}
然后,我调整了 GeneralMutator
实现的 where 子句:
impl<T> Mutator<T> for GeneralMutator
where T: HasVectorGenome,
T::BaseType : Rand
关于generics - 如何限制 Rust 中特征的通用实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35525505/