盒子<T>Rust 中的 &T
问题描述:
如果我有一个 Box<T>
,我该如何调用需要 trait 对象的函数?换句话说:
How do I call a function that expects a trait object if I have a Box<T>
instead? In other words:
trait T { ... }
fn func(t: &T) { ... }
fn some_other_func() {
b: Box<T>; // Provided
// These work, but is there a better way?
func( &*b ); // 1
func( Borrow::borrow(&b) ); // 2
}
1 和 2 似乎都错了.我是否遗漏了一些明显的东西?
Both 1 and 2 seem wrong. Am I missing something obvious?
答
&*foo
被称为重新借用",是惯用的.
&*foo
is called a "reborrow", and is idiomatic.