Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fn main() {
- // We can turn pointers (including function pointers) into integral types.
- let f: fn(i32) -> i32 = |x| {x+2};
- let g = f as usize;
- // But we can also discard percision. Why? What's the possible use case?
- let h = f as u8;
- // It seems like this should require:
- // let h = f as usize as u8;
- println!("g = {:#x}, h = {:#x}", g, h);
- unsafe {
- // A use case like this would be useful in some sort of FFI.
- let f = std::mem::transmute::<usize, fn(i32) -> i32>(g);
- println!("The answer is {}", f(40));
- }
- }
Add Comment
Please, Sign In to add comment