Advertisement
NLinker

Raw pointers in Rust

Jun 10th, 2017
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.80 KB | None | 0 0
  1. use std::rc::Rc;
  2.  
  3. trait Show {
  4.     fn show(&self);
  5. }
  6.  
  7. impl Show for u32 {
  8.     fn show(&self){
  9.         println!("{}:u32", self);
  10.     }    
  11. }
  12.  
  13. impl Show for i8 {
  14.     fn show(&self){
  15.         println!("{}:i8", self);
  16.     }
  17. }
  18.  
  19. impl Show for String {
  20.     fn show(&self){
  21.         println!("{}:String", self);
  22.     }    
  23. }
  24.  
  25.  
  26. fn main() {
  27.     let mut rcobj = Rc::new(0u32) as Rc<Show>;
  28.     rcobj.show();
  29.     rcobj = Rc::new(1i8) as Rc<Show>;
  30.     rcobj.show();
  31.     rcobj = Rc::new("It is Rc trait object".to_string()) as Rc<Show>;
  32.     rcobj.show();
  33.    
  34.     let mut rawobj = &2u32 as *const Show;
  35.     unsafe{(*rawobj).show();}
  36.     rawobj = &3i8 as *const Show;
  37.     unsafe{(*rawobj).show();}
  38.     rawobj = &"It is raw trait object".to_string() as *const Show;
  39.     unsafe{(*rawobj).show();}
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement