【发布时间】:2022-11-04 17:22:20
【问题描述】:
我有像let t1: &dyn T1 或let t1: Box<dyn T1> 这样的变量,或者来自外部库的这样的变量。我想将此变量用作另一个特征。所以,我有代码
fn another_function(let t2: Box<dyn T2>);
let t1: Box<dyn T1> = run_external_function();
another_function(t1);
如何将 Rust 中的特征转换为另一个特征?例如
trait T1{}
trait T2{}
impl T2 for dyn T1{}
let t1: &dyn T1 = ;
let t2: &dyn T2 = t1;
我在类似的code 中尝试过,但失败并出现错误
预期特征
T2,找到特征T1
【问题讨论】:
标签: rust