Rust - Swap Numbers
Swapping 2 numbers without a temp variable
This is a number swapping operation without using a temp variable written in Rust.
A swap with a temp variable would look like this:
fn swap_with_temp(mut a:i32, mut b:i32) -> (i32, i32) {
let temp = a;
a = b;
b = temp;
(a, b)
}
This creates another temp variable to contain either values in a
or b
, followed by doing the actual swap, and reassigning the value in the temp variable to the second variable.
This causes a slight waste of space as a temp variable of greater or equal size to the larger of the 2 variables is needed to do the swap.
A space efficient is to use mathematical operations +
, -
, *
, /
or Bitwise XOR
to do the swap.
Code
fn main() {
let a:i32 = 20;
let b:i32 = 10;
println!("Before swap a = {} and b = {}", a, b);
let mut result = swap(a, b);
println!("After swap1: a = {} and b = {}", result.0, result.1);
result = swap2(result.0, result.1);
println!("After swap2: a = {} and b = {}", result.0, result.1);
result = match result{
(0, 0) => (a, b),
(0, _) => (a, b),
(_, 0) => (a, b),
_ => swap3(result.0, result.1)
};
println!("After swap3: a = {} and b = {}", result.0, result.1);
result = swap_with_temp(result.0, result.1);
println!("After swap_with_temp: a = {} and b = {}", result.0, result.1);
}
fn swap(mut a: i32, mut b: i32) -> (i32, i32) {
a = a + b;
b = a - b;
a = a - b;
(a, b)
}
fn swap2(mut a:i32, mut b:i32) -> (i32, i32) {
a = a ^ b; // A XOR B
b = a ^ b; // A XOR B XOR B = A
a = a ^ b; // A XOR B XOR A = B
(a, b)
}
fn swap3(mut a:i32, mut b:i32) -> (i32, i32) {
a = a * b;
b = a / b;
a = a / b;
(a, b)
}
fn swap_with_temp(mut a:i32, mut b:i32) -> (i32, i32) {
let temp = a;
a = b;
b = temp;
(a, b)
}
# Output
Before swap a = 10 and b = 20
After swap1: a = 20 and b = 10
After swap2: a = 10 and b = 20
After swap3: a = 20 and b = 10
After swap_with_temp: a = 10 and b = 20
The above uses multiple methods to do a swap between 2 variables. However, this is not foolproof.
Potential Issues
Overflow
If either of the variables are larger than the specified type (i32
), there will be an overflow of variables:
fn main() {
let a:i32 = 1000000000;
let b:i32 = 2000000000;
}
Before swap a = 1000000000 and b = 2000000000
thread 'main' panicked at 'attempt to add with overflow', src/main.rs:17:9
Divide by zero
swap3
uses multiplication and division for the swap. Exception handling is required to prevent a divide by zero
error, if either of a
or b
is zero.
let a:i32 = 0;
let b:i32 = 10;
thread 'main' panicked at 'attempt to divide by zero', src/main.rs:33:9
A match
is done to prevent the use of the function if either numbers are zero:
result = match result{
(0, 0) => (a, b),
(0, _) => (a, b),
(_, 0) => (a, b),
_ => swap3(result.0, result.1)
};
println!("After swap3: a = {} and b = {}", result.0, result.1);
Comments ()