简体   繁体   English

Rust仿制药:预期 <T> 发现 <Foo>

[英]Rust generics: Expected <T> found <Foo>

I'm trying to use generics but I don't master that topic well enough and I get this error: 我正在尝试使用泛型,但是我对该主题的掌握不够好,因此出现此错误:

error: mismatched types:
expected `book::mdbook::MDBook<R>`,
found `book::mdbook::MDBook<renderer::html_handlebars::HtmlHandlebars>`
(expected type parameter,
found struct `renderer::html_handlebars::HtmlHandlebars`) [E0308]

This is the relevant code 这是相关代码

pub struct MDBook<R> where R: Renderer {
    title: String,
    author: String,
    config: BookConfig,
    pub content: Vec<BookItem>,
    renderer: R,
}

impl<R> MDBook<R> where R: Renderer {

    pub fn new(path: &PathBuf) -> Self {

        MDBook {
            title: String::from(""),
            author: String::from(""),
            content: vec![],
            config: BookConfig::new()
                        .set_src(path.join("src"))
                        .set_dest(path.join("book")),
            renderer: HtmlHandlebars::new(), // <---- ERROR HERE
        }
    }
}

The Renderer trait is empty at the moment and the implementation for HtmlHandlebars is 目前, Renderer特性是空的,并且HtmlHandlebars的实现是

pub struct HtmlHandlebars;

impl Renderer for HtmlHandlebars {

}

impl HtmlHandlebars {
    pub fn new() -> Self {
        HtmlHandlebars
    }
}

What am I doing wrong? 我究竟做错了什么?

impl<R> MDBook<R> where R: Renderer {

    pub fn new(path: &PathBuf) -> Self {

These lines claim that for all types R that implement Renderer , there is a method new(path) that returns MDBook<R> . 这些行声称, 对于实现Renderer 所有类型R ,都有一个方法new(path)返回MDBook<R> However, your implementation of the method always returns MDBook<HtmlHandlebars> regardless of what R is. 但是,无论R是什么,方法的实现总是返回MDBook<HtmlHandlebars>

You could add a trait bound to R (or a method to Renderer ) that allows constructing a value of type R in new . 您可以添加绑定到R的特征(或Renderer的方法),以允许在new构造类型R的值。 Alternatively, the method could accept the renderer as parameter, ie fn new(path: &Path, renderer: R) -> Self . 或者,该方法可以接受渲染器作为参数,即fn new(path: &Path, renderer: R) -> Self Either way, you need a way to get your hands on a renderer (ie, a value of type R ) inside new . 无论哪种方式,您都需要一种在new内部使用渲染器(即类型R的值)的方法。

If on the other hand you want to support something like this: 另一方面,如果您想支持以下内容:

let book = MDBook::new(path);
if some_condition {
    book.set_renderer(SomeOtherThing::new());
}

then generics are the wrong tool for the job, since they make the choice of renderer part of the static type of book . 那么泛型是完成这项工作的错误工具,因为泛型使选择渲染器成为book静态类型的一部分。 You can remove the R type parameter completely, keep your trait and simply store a trait object (likely Box<Renderer> ) in MDBook . 您可以完全删除R类型参数,保留特征并仅将特征对象 (可能是Box<Renderer> )存储在MDBook

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM