由于需求冲突,无法为生命周期参数“de”推断出合适的生命周期

Cannot infer an appropriate lifetime for lifetime parameter `'de` due to conflicting requirements

取下面的代码片段(在cargo中必须是运行,所以你可以将serde特性添加到num-bigint):

use num_bigint::BigInt;
use serde_derive::Deserialize;
use std::collections::HashMap;

#[derive(Debug, Deserialize)]
pub struct Trade<'a> {
    pub id: &'a str,
    pub price: BigInt,
    pub quantity: BigInt,
}

#[derive(Debug, Deserialize)]
pub struct TradeTable<'a> {
    pub trades: Vec<Trade<'a>>,
}

fn main() {
    let mut ether_trades: Vec<Trade> = Vec::new();
    ether_trades.push(Trade {
        id: "#1",
        price: BigInt::from(100),
        quantity: BigInt::from(2)
    });

    let mut trades: HashMap<&str, Vec<Trade>> = HashMap::new();
    trades.insert("ETH", ether_trades);
    println!("trades: {}", trades);
}

编译时出现这个错误:

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'de` due to conflicting requirements

还有这条笔记:

note: first, the lifetime cannot outlive the lifetime `'de` as defined on the impl at 34:17...

现在,我知道我需要让 'a'de 活得更短,但我该怎么做呢?我不知道 'de 生命周期是在哪里定义的。我试过像这样使用冒号:

'de: 'a

但这没有用。

请检查that one

TL;DR

#[derive(Debug, Deserialize)]
pub struct TradeTable<'a> {
    #[serde(borrow)]
    pub trades: Vec<Trade<'a>>,
}