小编Ola*_*ner的帖子

为什么我的 trait 需要一个生命周期参数?

作为一个 Rust 新手,我可能有点天真地开始了这个:

...

pub trait Decode<T> {
    fn decode_from<R: io::Read + ?Sized>(&mut self, stream: &mut R) -> T;
}

pub struct MQTTFrame<'a> {
    pub payload: &'a Vec<u8>,
}

pub struct MQTTFrameDecoder<'a> {
    pub payload: &'a mut Vec<u8>,
}

impl<'a> Decode<MQTTFrame<'a>> for MQTTFrameDecoder<'a> {
    fn decode_from<R: io::Read + ?Sized>(&mut self, stream: &mut R) ->    MQTTFrame<'a> {
        stream.read(&mut self.payload);
        MQTTFrame{ payload: self.payload }
    }
}
Run Code Online (Sandbox Code Playgroud)

其中,在尝试编译时,受到了欢迎:

src/testbed/mod.rs:31:24: 31:36 error: cannot infer an appropriate   lifetime for automatic coercion due to conflicting requirements …
Run Code Online (Sandbox Code Playgroud)

lifetime rust

5
推荐指数
1
解决办法
3247
查看次数

标签 统计

lifetime ×1

rust ×1