Created
December 28, 2015 12:38
-
-
Save anonymous/25ca168607ba72734f91 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Illustrate different aliasing behaviour between raw pointers and borrows. | |
// Inspect ASM or LLVM IR in release mode to see the difference. | |
#[inline(never)] | |
fn alias(a: *mut u32, b: *mut u32) { | |
unsafe { | |
*a += *b; | |
*b += *a; | |
} | |
} | |
#[inline(never)] | |
fn noalias(a: &mut u32, b: &mut u32) { | |
*a += *b; | |
*b += *a; | |
} | |
fn main() { | |
let mut a = 1; | |
let mut b = 1; | |
alias(&mut a, &mut b); | |
noalias(&mut a, &mut b); | |
println!("{}, {}", a, b); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment