【发布时间】:2019-12-24 19:07:28
【问题描述】:
为什么编译需要显式类型?我希望编译器能够理解Box<STest> 在第一个测试用例中等于Box<(dyn TTest + 'static)>,因为STest 实现了TTest 特征。是什么让编译器能够在第二种情况下将其隐式转换为 BoxedTTest,而在第一种情况下却不能这样做?
我在rustc 1.40.0(稳定)上使用rustc --edition 2018 mwe.rs 编译它,但--edition 2015 和rustc 1.42.0-nightly 上发生同样的错误。
trait TTest {}
struct STest {}
impl TTest for STest {}
type BoxedTTest = Box<dyn TTest>;
fn foo(_test: &BoxedTTest) {}
pub fn main() {
// expected trait TTest, found struct `STest`
let test1 = Box::new(STest {});
foo(&test1);
// OK
let test2: BoxedTTest = Box::new(STest {});
foo(&test2);
}
完整的错误如下:
error[E0308]: mismatched types
--> mwe.rs:13:9
|
13 | foo(&test1);
| ^^^^^^ expected trait TTest, found struct `STest`
|
= note: expected type `&std::boxed::Box<(dyn TTest + 'static)>`
found type `&std::boxed::Box<STest>`
【问题讨论】:
-
@Stargateur 似乎我有点搞砸了数据存储(因为我想存储任何在向量中实现 TTest 的东西)和处理数据之间的界限。让我尝试重构我的代码以将
dyn TTest带到任何地方,并且仅在与 Vec 交互时使用 Box'ed 版本。