I have a binding to a trait, and I want to cast it to the implementing struct so that I can call a method implemented on that struct. Is this possible? Below is code that illustrates what I'd like to do:
struct Struct {
num: u8
}
trait Trait {
fn trait_fn(&self) -> u8;
}
impl Trait for Struct {
fn trait_fn(&self) -> u8 {
self.num + 1
}
}
impl Struct {
fn struct_fn(&self) -> u8 {
self.num - 1
}
}
fn main() {
let t: Box<Trait> = Box::new(Struct { num: 5 });
let t_result = t.trait_fn();
let s_result = (*t as Struct).struct_fn(); // How can I do something like this?
println!("Trait Result: {}, Struct Result: {}", t_result, s_result);
}