# Idiom #21 Swap values of variables a and b

```std::mem::swap(&mut a, &mut b);
```
```procedure swap(a, b: in out Integer)
is
temp : Integer;
begin
temp := a;
a := b;
b := temp;
end swap;```
```a^=b;
b^=a;
a^=b;
```
```IDENTIFICATION DIVISION.
PROGRAM-ID. swap.
PROCEDURE DIVISION.
MOVE a    TO temp
MOVE b    TO a
MOVE temp TO b
STOP RUN.```
```#include <utility>
using std::swap;```
`swap(a, b);`
```var tmp = a;
a = b;
b = tmp;```
`(a, b) = (b, a);`
`import std.algorithm.mutation : swap;`
`swap(a,b);`
```auto temp = a;
a = b;
b = temp;```
```var tmp = a;
a = b;
b = tmp;```
`{a, b} = {b, a}`
```fun1(A, B) ->
do:something(),
fun2(B, A).

fun2(A, B) ->
now(A, is, B),
and(B, is, A),
keep:moving().```
```tmp = a
a = b
b = tmp```
`a, b = b, a`
```swap (a,b) = (b,a)
```
`[a, b] = [b, a];`
```var tmp = a;
a = b;
b = tmp;```
```T tmp = a;
a = b;
b = tmp;```
`(rotatef a b)`
`a, b = b, a`
```\$tmp = \$a;
\$a = \$b;
\$b = \$tmp;```
`list(\$b, \$a ) = [ \$a, \$b ];`
```tmp := a;
a := b;
b := tmp;```
`(\$a, \$b) = (\$b, \$a);`
`a, b = b, a`
`a, b = b, a`
```var a = 1
var b = 2
val tmp = a
a = b
b = tmp

```
```Sub Swap(Of T)(ByRef a As T, ByRef b As T)
Dim tmp as T
tmp = a
a = b
b = tmp
End Sub```

#### Idiom created by

programming-idioms.org