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