【发布时间】:2020-03-21 22:47:56
【问题描述】:
取如下代码sn-p(必须在cargo中运行,所以可以在num-bigint中添加serde功能):
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
但这没有用。
【问题讨论】:
-
由于你发布的代码中没有出现生命周期
'de,所以肯定有一段重要的代码丢失了。请添加相关代码,并显示完整的编译器错误。 -
@Aloso 就是这样,我不知道
'de在哪里。它也没有出现在我的源代码中。 -
错误信息应该告诉你文件、行号和列(例如
--> src/main.rs:29:18) -
哦,是的,对不起,错误与这一行有关:
pub trades: Vec<Trade<'a>>