我有一个特性的绑定,我想把它转换为实现结构,以便我可以调用在该结构上实现的方法。这可能吗?以下代码说明了我想做的事情:
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);
}