Rust 中的 Box<T> 到 &T
Box<T> to &T in Rust
如果我有一个 Box<T>
,我该如何调用需要特征对象的函数?也就是说:
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好像都不对。我是否漏掉了一些明显的东西?
&*foo
称为 "reborrow",是惯用语。
如果我有一个 Box<T>
,我该如何调用需要特征对象的函数?也就是说:
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好像都不对。我是否漏掉了一些明显的东西?
&*foo
称为 "reborrow",是惯用语。