use std::rc::Rc;
trait Show {
fn show(&self);
}
impl Show for u32 {
fn show(&self){
println!("{}:u32", self);
}
}
impl Show for i8 {
fn show(&self){
println!("{}:i8", self);
}
}
impl Show for String {
fn show(&self){
println!("{}:String", self);
}
}
fn main() {
let mut rcobj = Rc::new(0u32) as Rc<Show>;
rcobj.show();
rcobj = Rc::new(1i8) as Rc<Show>;
rcobj.show();
rcobj = Rc::new("It is Rc trait object".to_string()) as Rc<Show>;
rcobj.show();
let mut rawobj = &2u32 as *const Show;
unsafe{(*rawobj).show();}
rawobj = &3i8 as *const Show;
unsafe{(*rawobj).show();}
rawobj = &"It is raw trait object".to_string() as *const Show;
unsafe{(*rawobj).show();}
}
Comments