rust - as_deref 函数不会改变我的具体类型
问题描述
我想将我的转换Option<&String>
为Option<&str>
withas_deref()
但编译器似乎误解了我的意图。如果我用它替换它就.map(|e| e.as_str())
可以了。
use std::collections::HashMap;
trait SipField {
fn from(&self) -> Option<&str>;
}
impl SipField for HashMap<String, Vec<String>> {
fn from(&self) -> Option<&str> {
self.get("from")
.or_else(|| self.get("f"))
.and_then(|i| i.first())
.as_deref()
}
}
error[E0308]: mismatched types
--> src/lib.rs:9:9
|
9 | / self.get("from")
10 | | .or_else(|| self.get("f"))
11 | | .and_then(|i| i.first())
12 | | .as_deref()
| |_______________________^ expected `str`, found struct `std::string::String`
|
= note: expected enum `std::option::Option<&str>`
found enum `std::option::Option<&std::string::String>`
解决方案
推荐阅读
- python - 遍历每个不和谐频道,并查找某个频道是否存在 discord.py
- ios - 最大限度地减少来自 Google Cloud 的延迟流式视频
- python - 从教程中实现玩具代码的问题
- kubernetes-helm - 使用节点选择器舵图将 pod 分配到特定节点池
- reactjs - 使用reactjs删除mongodb中的项目不起作用
- java - 无法使用 Spring Boot 设置 H2 JPA 数据库 - 错误:没有可用于自动装配的 bean
- python - 如何在 Google Cloud Run 中生成 Blob 签名 URL?
- node.js - 节点模块大小?
- java - 用spring boot rest api反应路由器
- javascript - 功能组件在浏览器刷新时中断,因为从 redux 状态中提取的对象不能按时使用