Box<T> to &T in Rust

风格不统一 提交于 2019-12-12 13:17:22

问题


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
}

Both 1 and 2 seem wrong. Am I missing something obvious?


回答1:


&*foo is called a "reborrow", and is idiomatic.



来源:https://stackoverflow.com/questions/32084849/boxt-to-t-in-rust

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!