繁体   English   中英

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

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

我正在尝试使用泛型,但是我对该主题的掌握不够好,因此出现此错误:

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]

这是相关代码

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
        }
    }
}

目前, Renderer特性是空的,并且HtmlHandlebars的实现是

pub struct HtmlHandlebars;

impl Renderer for HtmlHandlebars {

}

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

我究竟做错了什么?

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

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

这些行声称, 对于实现Renderer 所有类型R ,都有一个方法new(path)返回MDBook<R> 但是,无论R是什么,方法的实现总是返回MDBook<HtmlHandlebars>

您可以添加绑定到R的特征(或Renderer的方法),以允许在new构造类型R的值。 或者,该方法可以接受渲染器作为参数,即fn new(path: &Path, renderer: R) -> Self 无论哪种方式,您都需要一种在new内部使用渲染器(即类型R的值)的方法。

另一方面,如果您想支持以下内容:

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

那么泛型是完成这项工作的错误工具,因为泛型使选择渲染器成为book静态类型的一部分。 您可以完全删除R类型参数,保留特征并仅将特征对象 (可能是Box<Renderer> )存储在MDBook

暂无
暂无

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

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