盒子<T>Rust 中的 &T [英] Box<T> to &T in Rust
本文介绍了盒子<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.
这篇关于盒子<T>Rust 中的 &T的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文