Types 在结构内调用装箱函数时,类型不匹配

Types 在结构内调用装箱函数时,类型不匹配,types,rust,type-mismatch,Types,Rust,Type Mismatch,我试图根据下面的struct Problem勾勒出运行“problems”的一般方法。这些结构包括测试输入/输出和解算器函数。我的目标是通过与标准I/O交互,或通过某些测试仪函数来运行这些问题,这些测试仪函数根据给定的test\u输入/test\u输出验证解算器的有效性。此处的示意图仅包括测试仪功能 当我试图编译它时,我得到类型不匹配: problem.rs:43:22: 43:27 error: mismatched types: expected `R`, found `std::

我试图根据下面的
struct Problem
勾勒出运行“problems”的一般方法。这些结构包括测试输入/输出和解算器函数。我的目标是通过与标准I/O交互,或通过某些测试仪函数来运行这些问题,这些测试仪函数根据给定的
test\u输入
/
test\u输出
验证
解算器
的有效性。此处的示意图仅包括测试仪功能

当我试图编译它时,我得到类型不匹配:

problem.rs:43:22: 43:27 error: mismatched types:
 expected `R`,
    found `std::io::cursor::Cursor<&[u8]>`
(expected type parameter,
    found struct `std::io::cursor::Cursor`) [E0308]
problem.rs:43     (problem.solver)(input, &mut output);
                                   ^~~~~
problem.rs:43:22: 43:27 help: run `rustc --explain E0308` to see a detailed explanation
problem.rs:43:29: 43:40 error: mismatched types:
 expected `&mut W`,
    found `&mut std::io::cursor::Cursor<collections::vec::Vec<u8>>`
(expected type parameter,
    found struct `std::io::cursor::Cursor`) [E0308]
problem.rs:43     (problem.solver)(input, &mut output);
                                          ^~~~~~~~~~~
problem.rs:43:29: 43:40 help: run `rustc --explain E0308` to see a detailed explanation
error: aborting due to 2 previous errors

您定义
问题
结构的方式意味着
R
W
在实例化
问题时是固定的。在
test\u solver
中,您允许调用方指定所需的任何
R
和任何
W
,但随后您尝试将特定的
光标
类型传递给
(problem.solver)

您可以通过两种方式解决此问题:

1) 不要将
test\u solver
定义为通用函数。请注意,对于
R
W
,您不会多次调用
(problem.solver)

fn test_solver<'a>(problem: Problem<'a, std::io::Cursor<&'a [u8]>, std::io::Cursor<Vec<u8>>>)
{
    let input = std::io::Cursor::new(problem.test_input.as_bytes());
    let mut output = std::io::Cursor::new(Vec::<u8>::new());

    (problem.solver)(input, &mut output);
    assert_eq!(problem.test_output.as_bytes(), &*output.into_inner());
}

您定义
问题
结构的方式意味着
R
W
在实例化
问题时是固定的。在
test\u solver
中,您允许调用方指定所需的任何
R
和任何
W
,但随后您尝试将特定的
光标
类型传递给
(problem.solver)

您可以通过两种方式解决此问题:

1) 不要将
test\u solver
定义为通用函数。请注意,对于
R
W
,您不会多次调用
(problem.solver)

fn test_solver<'a>(problem: Problem<'a, std::io::Cursor<&'a [u8]>, std::io::Cursor<Vec<u8>>>)
{
    let input = std::io::Cursor::new(problem.test_input.as_bytes());
    let mut output = std::io::Cursor::new(Vec::<u8>::new());

    (problem.solver)(input, &mut output);
    assert_eq!(problem.test_output.as_bytes(), &*output.into_inner());
}

请尽量减少你的代码——它包含了很多与问题无关的东西。好的,删除了很多代码。不确定我是否可以删除更多而不使其变得不那么清晰。请尽量减少您的代码--它包含许多与问题无关的内容。好的,删除了相当多的代码。不确定我是否可以删除更多而不使其变得不那么清晰。
use std::io::prelude::*;
use problems::Problem;

mod problems {
    use std::io::prelude::*;

    pub struct Problem<'a>
    {
        pub test_input: &'a str,
        pub test_output: &'a str,
        pub solver: Box<fn(&mut BufRead, &mut Write)>,
    }

    pub mod a_problem {
        use std::io::prelude::*;
        use super::Problem;

        pub fn getProblem<'a>() -> Problem<'a>
        {
            Problem {
                test_input: unimplemented!(),
                test_output: unimplemented!(),
                solver: Box::new(solver),
            }
        }

        fn solver(input: &mut BufRead, output: &mut Write)
        {
            unimplemented!();
        }
    }
}

fn test_solver(problem: Problem)
{
    let mut input = std::io::Cursor::new(problem.test_input.as_bytes());
    let mut output = std::io::Cursor::new(Vec::<u8>::new());

    (problem.solver)(&mut input, &mut output);
    assert_eq!(problem.test_output.as_bytes(), &*output.into_inner());
}

fn main() {
    let problem = problems::a_problem::getProblem();
    test_solver(problem);
}