【发布时间】:2015-07-18 11:57:19
【问题描述】:
我正在尝试使用泛型,但我没有很好地掌握该主题,我收到此错误:
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
}
}
我做错了什么?
【问题讨论】:
-
顺便说一句,
&PathBuf和&String一样没用。你想要&Path(道德上等同于&str)。 -
好的,谢谢!我不知道:)