This page looks best with JavaScript enabled

Rust - Swap Numbers

 ·  ☕ 3 min read  ·  🐺 Devoalda

Introduction

This is a number swapping operation without using a temp variable written in Rust.

A swap with a temp variable would look like this:

1
2
3
4
5
6
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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)
}
1
2
3
4
5
6
# 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:

1
2
3
4
fn main() {
    let a:i32 = 1000000000;
    let b:i32 = 2000000000;
}
1
2
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.

1
2
let a:i32 = 0;
let b:i32 = 10;
1
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:

1
2
3
4
5
6
7
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);
Share on

Devoalda
WRITTEN BY
Devoalda
Technophile