由于需求冲突,无法为生命周期参数'de'推断适当的生命周期

时间:2020-03-21 22:47:56

标签: rust lifetime

采用以下代码段(必须在货运中运行,因此您可以将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

但这没用。

1 个答案:

答案 0 :(得分:1)

请检查that one

TL; DR

mongo admin -u root -p root