RS.CLIPPY.MANUAL_SWAP

Manual swap of two variables

This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: manual_swap. Copyright ©2025 The Rust Team. All rights reserved.

What it does

Checks for manual swapping.

Note that the lint will not be emitted in const blocks, as the suggestion would not be applicable.

Why is this bad?

The std::mem::swap function exposes the intent better without deinitializing or copying either variable.

Example

let mut a = 42;
let mut b = 1337;

let t = b;
b = a;
a = t;

Use std::mem::swap():

let mut a = 1;
let mut b = 2;
std::mem::swap(&mut a, &mut b);