Skip to content

Instantly share code, notes, and snippets.

@mykhailokrainik
Created October 2, 2018 08:24
Show Gist options
  • Save mykhailokrainik/0ced82c341226ad958efcf2987009d15 to your computer and use it in GitHub Desktop.
Save mykhailokrainik/0ced82c341226ad958efcf2987009d15 to your computer and use it in GitHub Desktop.
swap two digits without use third
fn main() {
let mut x = 2;
let mut y = 1;
println!("before x {} y {}", x, y); // before x 2 y 1
if x != y {
x = x ^ y;
y = x ^ y;
x = x ^ y;
}
println!("after x {} y {}", x, y); // after x 1 y 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment