【问题标题】:How to tell which generic type Rust is unable to infer?如何判断 Rust 无法推断出哪种泛型类型?
【发布时间】:2023-03-27 03:02:01
【问题描述】:

我在这种情况下遇到了一个问题(这里的代码是可构建的):

extern crate rand;
use rand::{Isaac64Rng, SeedableRng, Rng};
pub trait GeneticAlgorithm<R, Ins, C> : Clone where R: Rng {
    fn mate(parents: (&Self, &Self), rng: &mut R) -> Self;
    fn mutate<F>(&mut self, rng: &mut R, mutator: F) where F: FnMut(&mut Ins);
    fn call<F>(&self, program: F) where F: FnOnce(&C);
}

pub struct Mep<Ins> {
    instructions: Vec<Ins>,
    unit_mutate_size: usize,
    crossover_points: usize,
}

impl<Ins> Mep<Ins> {
    //Generates a new Mep with a particular size and takes a closure to generate random instructions
    pub fn new<I>(unit_mutate_size: usize, crossover_points: usize, instruction_iter: I) -> Mep<Ins>
        where I: Iterator<Item=Ins> {
        Mep{instructions: instruction_iter.collect(), unit_mutate_size: unit_mutate_size,
            crossover_points: crossover_points}
    }
}

impl<Ins> Clone for Mep<Ins>
    where Ins: Clone {
    fn clone(&self) -> Self {
        Mep{instructions: self.instructions.clone(), unit_mutate_size: self.unit_mutate_size,
            crossover_points: self.crossover_points}
    }
}

impl<R, Ins> GeneticAlgorithm<R, Ins, Vec<Ins>> for Mep<Ins> where R: Rng, Ins: Clone {
    fn mate(parents: (&Mep<Ins>, &Mep<Ins>), rng: &mut R) -> Mep<Ins> {}
    fn mutate<F>(&mut self, rng: &mut R, mut mutator: F) where F: FnMut(&mut Ins) {}
    fn call<F>(&self, program: F) where F: FnOnce(&Vec<Ins>) {
        program(&self.instructions);
    }
}

fn main() {
    let mut rng = Isaac64Rng::from_seed(&[1, 2, 3, 4]);
    let (a, b) = {
        let mut clos = || Mep::new(3, 3, rng.gen_iter::<u32>().map(|x| x % 10).take(10));
        (clos(), clos())
    };
    let mut c = Mep::mate((&a, &b), &mut rng);
    c.mutate(&mut rng, |ins: &mut u32| *ins = 2);
    c.call(|x: &Vec<u32>| panic!());
}

Rust 声称它无法在某处推断类型,但如果这是问题,我不确定如何指定闭包的类型,我也无法确定是哪个特定的泛型参数导致了问题:

main.rs:48:7: 48:36 error: unable to infer enough type information about `_`; type annotations or generic parameter binding required [E0282]
main.rs:48     c.call(|x: &Vec<u32>| panic!());
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

需要指定哪个通用参数以及如何确定?如果无法推断,如何指定预期的特征:GeneticAlgorithm&lt;Isaac64Rng, u32, Vec&lt;u32&gt;&gt;

如果有人想自己构建原始代码,我是hosting it on GitHub (commit b0b24482fb7fc71da9c23cd1481ea09c9edd867e)

【问题讨论】:

标签: generics rust


【解决方案1】:
impl<R, Ins> GeneticAlgorithm<R, Ins, Vec<Ins>> for Mep<Ins> where R: Rng, Ins: Clone {
    // ...
}

impl 块为Mep&lt;Ins&gt; 实现GeneticAlgorithm,以实现R 的所有可能值。这意味着对于特定的Mep&lt;Ins&gt;GeneticAlgorithm 特征有多种实现。当您调用 matemutate 方法时,编译器能够从参数解析特定实现,但是当您调用 call 时,编译器无法解析特定实现,因为 R 不受约束.

要解决此问题,请将R 泛型参数移至matemutate 方法。

pub trait GeneticAlgorithm<Ins, C> : Clone {
    fn mate<R>(parents: (&Self, &Self), rng: &mut R) -> Self where R: Rng;
    fn mutate<R, F>(&mut self, rng: &mut R, mutator: F) where F: FnMut(&mut Ins), R: Rng;
    fn call<F>(&self, program: F) where F: FnOnce(&C);
}

impl<Ins> GeneticAlgorithm<Ins, Vec<Ins>> for Mep<Ins> where Ins: Clone {
    fn mate<R>(parents: (&Mep<Ins>, &Mep<Ins>), rng: &mut R) -> Mep<Ins> where R: Rng { panic!() }
    fn mutate<R, F>(&mut self, rng: &mut R, mut mutator: F) where F: FnMut(&mut Ins), R: Rng { panic!() }
    fn call<F>(&self, program: F) where F: FnOnce(&Vec<Ins>) {
        program(&self.instructions);
    }
}

【讨论】:

    猜你喜欢
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多