Guest User

Untitled

a guest
May 21st, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. fn main() {
  2. // We can turn pointers (including function pointers) into integral types.
  3. let f: fn(i32) -> i32 = |x| {x+2};
  4. let g = f as usize;
  5.  
  6. // But we can also discard percision. Why? What's the possible use case?
  7. let h = f as u8;
  8. // It seems like this should require:
  9. // let h = f as usize as u8;
  10.  
  11. println!("g = {:#x}, h = {:#x}", g, h);
  12. unsafe {
  13. // A use case like this would be useful in some sort of FFI.
  14. let f = std::mem::transmute::<usize, fn(i32) -> i32>(g);
  15. println!("The answer is {}", f(40));
  16. }
  17. }
Add Comment
Please, Sign In to add comment