Skip to content

Use idiomatic std::mem::take instead of std::mem::replace #157245

Description

@ShinysLove

In several modules, we are resetting structures to their default values using std::mem::replace(&mut var, Default::default()). While this works, it is less idiomatic and more verbose than necessary.
Describe the solution you'd like
Replace these occurrences with std::mem::take(&mut var), which is the standard and more concise way to achieve the exact same behavior for types that implement Default.

Example

// Before
let old_value = std::mem::replace(&mut self.buffer, Vec::new());

// After
let old_value = std::mem::take(&mut self.buffer);

This change improves code readability and aligns with Rust best practices (clippy lint: mem_replace_with_default).

Metadata

Metadata

Assignees

Labels

C-cleanupCategory: PRs that clean code up or issues documenting cleanup.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions