Advertisement
Guest User

Untitled

a guest
Oct 10th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.43 KB | None | 0 0
  1. fn main() {
  2. let a=4u32;
  3. let b = &a as *const u32;
  4. let b = b as *mut u32;
  5. unsafe {
  6. test(&a, &mut *b);
  7. println!("{}",*b);
  8. }
  9. }
  10.  
  11. fn test(a:&u32, b:&mut u32)->u32{
  12. if *a < 5 {
  13. *b+= 20;
  14. }//compiler assumes a variable cannot be
  15. //aliased if it's mutable, so it puts an else
  16. //here
  17. if *a > 10 {
  18. *b+=10;
  19. }
  20. *b //expected result: 34, actual result
  21. // (in release) 24
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement