NLinker icon

Raw pointers in Rust

NLinker | PRO | 06/10/17 10:03:52 AM UTC | 0 ⭐ | 991 👁️ | Never ⏰ | []
Rust |

800 B

|

None

|

0 👍

/

0 👎

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