rust - 由于需求冲突,无法为生命周期参数“de”推断适当的生命周期
问题描述
采取以下代码片段(必须在 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
live 短于'de
,但我该怎么做呢?我不知道'de
生命周期在哪里定义。我尝试使用这样的冒号:
'de: 'a
但这没有用。
解决方案
请检查那个
TL;博士
#[derive(Debug, Deserialize)]
pub struct TradeTable<'a> {
#[serde(borrow)]
pub trades: Vec<Trade<'a>>,
}
推荐阅读
- protractor - 我们可以使用量角器工具自动化非英语网站吗
- regex - 使用正则表达式捕获多值
- python - 如何单击按钮以播放与条目小部件中的文本输入相关的不同声音?
- ssl - 如何在使用 openssl 的专用网络上的服务器上启用 SSL?
- elasticsearch - 用于类似于拆分的带状疱疹的 Elasticsearch 处理器?
- sql-server - 无法将 Guidewire 连接到 SQL Server 数据库
- swift - 如何使用 Swift 从 Firebase Cloud Firestore 中获取最新的文档
- git - 恢复远程分支上的提交
- java - 令人困惑的 String.split("") 行为
- reactjs - eCharts 5 轴标签重叠