Skip to content

Instantly share code, notes, and snippets.

Created December 28, 2015 12:38
Show Gist options
  • Save anonymous/25ca168607ba72734f91 to your computer and use it in GitHub Desktop.
Save anonymous/25ca168607ba72734f91 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
// 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