How to swap two numbers?

Viewed 495

I'm trying to swap two integers in Rust without any library.

fn main() {
    let mut a = 5;
    let mut b = 6;
    swap(&mut a, &mut b);
    println!("{}, {}", a, b); // expecting 6, 5
}

First I've tried:

fn swap(mut a: &mut u32, mut b: &mut u32) {
    (a, b) = (b, a); // destructuring assignments are not currently supported
}

Then:

fn swap(mut a: &mut u32, mut b: &mut u32) {
    let temp = a;
    a = b;
    b = temp;
}
// got error[E0623]: lifetime mismatch

Finally:

fn swap<'a>(mut a: &'a mut u32, mut b: &'a mut u32) {
    let temp = a;
    a = b;
    b = temp;
}
// still does not work; when this scope ends a and b in `main()` are still 5 and 6 respectively
// got warning: value assigned to `a` is never read (for `b` as well)

I don't know what's wrong; last implementation looks cumbersome to me but I think it should work as I am passing mutable reference to function, but it does not work.

3 Answers

You can dereference references:

fn swap(a: &mut u32, b: &mut u32) {
    let t: u32 = *a; // Read the integer with `*a`
    *a = *b; // Write to the reference with `*a = <stuff>`
    *b = t;
}

fn main() {
    let mut a = 6;
    let mut b = 7;
    
    println!("{}, {}", a, b);
    swap(&mut a, &mut b);
    println!("{}, {}", a, b);
}

Output:

6, 7
7, 6

Playground

For copyable types this is quite succinct (if you prefer not to use std::mem::swap:

fn swap<T: Copy>(a: T, b: T) -> (T, T) {
    (b, a)
}

fn main() {
    let (a, b) = (1, 2);
    let (a, b) = swap(a, b);

    println!("A: {}, B: {}", a, b);

    // Note you can also just do this
    let (a, b) = (b, a);

    println!("A: {}, B: {}", a, b);
}
Related