rust - Rust:将特征对象转换为不同的类型
问题描述
有什么方法可以安全地将特征对象(dyn A
)转换为另一种类型(dyn B
)?
trait A {}
trait B: A {}
impl <T: A> B for T {}
fn cast(x: &dyn A) -> &dyn B {
x
}
error[E0308]: mismatched types
--> src/lib.rs:6:3
|
6 | x
| ^ expected trait `B`, found trait `A`
|
= note: expected type `&dyn B`
found type `&dyn A`
解决方案
推荐阅读
- javascript - JS:仅对对象数组的一个字段进行排序
- powerbi - 如何在特定条件下用 2 种不同颜色为同一折线图着色?
- amazon-web-services - 如何将现有的 EC2 实例附加到 terraform 中的 Auto Scaling 组?
- angular - primeng - 在页面加载时显示多选下拉菜单?
- python - Python Pandas Dataframe 未过滤所有有效记录
- python - 作为 2 个表的结果创建一个表
- sql - 轧制产品剩余间隙填充
- java - 在 Spring Boot 项目中找不到页面
- google-cloud-platform - 使用对话流部署多个代理
- javascript - Flask Web 应用程序中的 Ajax 发布调用期间出现错误 502