-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
make_
, to_
, and into_
methods for in-place, cloning, and "eating" conversions, respectively
#2447
Comments
AFAIK, this distinction already exists in |
I'm looking for |
ideally, Regarding the guidelines, |
No, Ideally, it should be equivalent to calling |
Ok, I see what you mean and how that could be useful for method chaining and potentially performance, but I believe the reason that |
There is no reason to consume the string, but you should still be able to so the compiler can optimize it away. It would be nice if to_ascii_lowercase could be optimized such that there are 2 impls for it, one that takes self, other that takes &self. Perhaps something along the lines of: impl SomeDummyTrait for String {
fn to_ascii_lowercase(self) -> String {
self.make_ascii_lowercase();
self
}
}
impl SomeDummyTrait for &String {
fn to_ascii_lowercase(self) -> String {
self.clone().to_ascii_lowercase()
}
} But since we can't have that, as it would break backwards compatibility, I feel like into_ascii_lowercase is the way to go. Perhaps future versions of the language can support syntax for doing this - checking if a value is never used again, and choosing a more optimized solution. |
I'm going to close this here and suggest the overall convention suggestion move to updating the API guidelines, specifically https://rust-lang.github.io/api-guidelines/naming.html#ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv If you have a particular method that you think should exist, please just make a PR for it and the decision can be made there -- there's no reason to keep an issue open here about it. |
what about stuff like make_mut and whatnot? those aren't in the guidelines? |
@SoniEx2 Then you can propose an update to the guidelines in the guidelines repo. |
I would like the stdlib to follow the following rules for conversion methods:
make_
()
orResult<(), E>
.to_
T
orResult<T, E>
.into_
self
/mut self
).T
orResult<T, E>
.Example:
The main benefits of the
into_
methods is that the compiler knows how to optimize them, as they don't necessarily hit the allocator (certainly not in theinto_ascii_lowercase
case), which can be nice for some things.(Accidentally put this in the wrong repo, sorry! Thanks for having
rfcs
andrust
repos btw because they have the same/similar length)The text was updated successfully, but these errors were encountered: