RS.CLIPPY.CLEAR_WITH_DRAIN

Calling `drain` in order to `clear` a container

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

What it does

Checks for usage of .drain(..) for the sole purpose of clearing a container.

Why is this bad?

This creates an unnecessary iterator that is dropped immediately.

Calling .clear() also makes the intent clearer.

Example

let mut v = vec![1, 2, 3];
v.drain(..);

Use instead:

let mut v = vec![1, 2, 3];
v.clear();