RS.CLIPPY.MEM_REPLACE_WITH_DEFAULT

Replacing a value of type `T` with `T::default()` instead of using `std::mem::take`

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

What it does

Checks for std::mem::replace on a value of type T with T::default().

Why is this bad?

std::mem module already has the method take to take the current value and replace it with the default value of that type.

Example

let mut text = String::from("foo");
let replaced = std::mem::replace(&mut text, String::default());

Is better expressed with:

let mut text = String::from("foo");
let taken = std::mem::take(&mut text);

Configuration

  • msrv: The minimum rust version that the project supports. Defaults to the rust-version field in Cargo.toml

    (default: current version)