Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a Box<T> to &T?

Tags:

rust

traits

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?

like image 726
anjruu Avatar asked Sep 08 '25 02:09

anjruu


1 Answers

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

like image 141
Veedrac Avatar answered Sep 09 '25 15:09

Veedrac