Generics Error: expected type parameter, found struct

前端 未结 2 1491
你的背包
你的背包 2020-11-30 15:34

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 t

2条回答
  •  孤独总比滥情好
    2020-11-30 15:53

    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 . 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  Rustmark  {
        fn new() -> Rustmark  {
            Rustmark {
                parser: P::default(),
                renderer: R:default(),
            }
        }
    }
    

    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.

提交回复
热议问题