-
Notifications
You must be signed in to change notification settings - Fork 13.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add mem::{take, replace, swap}
to the prelude
#126785
Conversation
rustbot has assigned @Mark-Simulacrum. Use |
Some changes occurred in src/tools/clippy cc @rust-lang/clippy |
As an educator, I can definitely see the value here. I think people tend to forget these functions, buried in |
Not a blocker, but I personally like to prefix those functions with |
The job Click to see the possible cause of the failure (guessed by this bot)
|
I don't think we should do this. At the very least, we need to consider alternative directions in language evolution. Yeah, the compiler's name-res might be able to handle it, but what about the human's? ptr::replace and ptr::swap exist, and there are proposals to allow importing type-qualified or trait-qualified functions to use directly, in lieu of having additional free functions. If such happens, the fact that |
Prior art, adding |
I personally actively avoid importing |
This is the case for myself and countless others as well. If anything, I would like to see For what it's worth, I'm not positive the compiler is currently able to handle the name resolution shadowing for modules the same way it can for functions. I say this because there are not currently any modules in the prelude. |
☔ The latest upstream changes (presumably #126951) made this pull request unmergeable. Please resolve the merge conflicts. |
Given the feedback (which I agree with!), I'm going to close this PR. |
This adds the following 3 functions from
core::mem
to the prelude:The main goal is to make Rust easier to teach, while also making common code patterns easier to write. Many new Rust users coming from other languages find Rust's ownership system hard to use and a part of that is because it does not allow leaving values in an uninitialized state after moving them, even temporarily. While the tools to work around this are available in
core::mem
, they are not very discoverable for new users. By treating these functions more like keywords (similar todrop
), we can encourage the inclusion of these functions in teaching materials and expose new Rust users to them.These 3 functions are also very widely used in production Rust code so even experienced Rust users will benefit from this change. For example, in the Rust compiler there are:
mem::replace
.mem::take
.mem::swap
.