泛型错误:预期类型参数,找到结构 [英] Generics Error: expected type parameter, found struct

查看:30
本文介绍了泛型错误:预期类型参数,找到结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始了一个新项目,我希望尽可能模块化,我的意思是我希望将来能够用其他部分替换某些部分.这似乎是 traits 的完美用法,目前我有这个代码:

I started a new project, where I want to be as modular as possible, by that I mean that I would like to be able to replace some parts with others in the future. This seems to be a perfect use for traits, at the moment I have this code:

mod parser;
mod renderer;
mod renderers;

use parser::MarkParser;
use renderer::MarkRenderer;

struct Rustmark <P: MarkParser, R: MarkRenderer> {
    parser: P,
    renderer: R,
}


impl <P: MarkParser, R: MarkRenderer> Rustmark <P, R> {
    fn new() -> Rustmark <P, R> {
        Rustmark {
            parser: parser::DefaultParser::new(),
            renderer: renderers::HTMLRenderer::new(),
        }
    }

    fn render(&self, input: &str) -> &str {
        self.renderer.render(self.parser.parse(input))
    }
}

但是我遇到了几个错误,主要是这个:

But I get a couple of errors, mainly this one:

错误: 类型不匹配:预期 Rustmark,找到 Rustmark(预期的类型参数,找到结构 parser::DefaultParser) [E0308]

error: mismatched types: expected Rustmark<P, R>, found Rustmark<parser::DefaultParser, renderers::html::HTMLRenderer> (expected type parameter, found struct parser::DefaultParser) [E0308]

还有几个像这样的终生错误:

And a couple of lifetime errors like this one:

错误:由于需求冲突,无法为自动强制推断合适的生命周期

error: cannot infer an appropriate lifetime for automatic coercion due to conflicting requirements

帮助:考虑使用显式生命周期参数,如下所示:fn parse<'a>(&'a self, input: &'a str) ->&str

help: consider using an explicit lifetime parameter as shown: fn parse<'a>(&'a self, input: &'a str) -> &str

我尝试了多次调整以使其正常工作,但似乎没有一个能安抚编译器.所以我想知道这是否是正确的方法,以及我可以做些什么来使其发挥作用.

I have tried multiple tweaks to make it work, but none of them seemed to appease the compiler. So I wanted to know if this is the right approach and what I could do to to make it work.

推荐答案

第一个错误:您创建了一个 Rustmark 对象,其字段 parser 的类型为 DefaultParserHTMLRenderer 类型的字段 renderer,但该函数应返回 Rustmark .一般来说,P 不是 DefaultParser 类型,R 不是 HTMLRenderer 类型,所以它永远不会编译.如果您想要拥有正确类型的默认值,一个好的解决方案是绑定 PR 以实现 Default trait,这样:

First error: you create a Rustmark object with the field parser of type DefaultParser and the field renderer of type HTMLRenderer, but the function is expected to return Rustmark <P, R>. In general P is not of type DefaultParser and R is not of type HTMLRenderer, so it will never compile. A good solution if you want have default values of the right type is to bound P and R to implement the Default trait, this way:

use std::default:Default;

impl <P: MarkParser + Default, R: MarkRenderer + Default> Rustmark <P, R> {
    fn new() -> Rustmark <P, R> {
        Rustmark {
            parser: P::default(),
            renderer: R:default(),
        }
    }
}

第二个错误:主要问题是您返回的引用可能会在 render 方法(您在内部 中分配的 String渲染方法可能).编译器告诉您它不知道该引用指向的对象的生命周期,因此它不能保证该引用是有效的.您可以指定生命周期参数,但在您的情况下,最好的解决方案可能是返回 String 对象本身,而不是引用.

Second error: that main problem is that you return a reference of something that probably will die inside the rendermethod (the String you allocate in the inner rendermethod probably). The compiler is telling you that it doesn't know the lifetime of the object that is pointed by that reference, so it cannot guarantee that the reference is valid. You can specify a lifetime parameter but in your case probably the best solution is to return the String object itself, not a reference.

这篇关于泛型错误:预期类型参数,找到结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆