-
Notifications
You must be signed in to change notification settings - Fork 13k
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
RFC: standardizing on naming functions that convert from one type to another #7087
Comments
This is an important consideration to keep us all sane and free from constant naming squabbles. I don't think there's really much to debate here. |
Hm, on the one hand using the short prefixes for cheap operations makes sense... Plus "into" doesn't really make sense as a prefix for doing a copy, I think.
|
nominating |
Could this be done with some traits? trait MoveConvert<T> {
fn be(self)->T;
}
trait RefConvert<T> {
fn as(&self)->&T;
}
trait CopyConvert<T> {
fn to(&self)->T;
} Of course the 'as' method would need another name as it is currently in use as a keyword. |
My apologies, I totally missed that even though it is in the original issue description. I suppose you can count my opinion as one in support of that particular solution. |
I'd like to get this and other stylistic issues that affect std resolved this year and written up as coding standards (better than https://github.com/mozilla/rust/wiki/Note-style-guide) |
accepted for backwards-compatible |
Another bikeshed option. We could rename methods like |
It seems the codebase is slowly moving into the However, the problem is that If
The main problem is that the current
Regardless of concrete name, we should also make this a guideline:
|
One more bikeshed option: |
We should probably also decide on something for (e.g.) Currently we're just using |
@huonw I don't think that should be relevant. it makes an copy, whether that's actually expensive depends on the type. |
This was opened a year ago and been last updated more then 6 month ago, is it still outstanding? |
The guidelines have now made Closing as resolved. |
Allow allman style braces in `suspicious_else_formatting` fixes: rust-lang#3864 Indentation checks could be added as well, but the lint already doesn't check for it. changelog: Allow allman style braces in `suspicious_else_formatting`
It's time to handle another one of the hardest things in computer science, how to name conversion functions. Rust supports essentially three ways of converting from one type to another. First there is doing a copy, such as these in the
str
mod:Then we have some that can do a no-allocation moves, such as
either
's:Finally, we have ones that can get a no-copy reference, as in
str
's:While they share similar names, it's not consistent what exactly is happening in each case. I would like to standardize on a style so I can know how to name things for the standard library.
Some options that came up in IRC are:
as
for conversions, andto
for moves or copies. Simple, but then we end up with functions named liketo_str_consume
to distinguish between the two cases, which is a little ugly.as
for conversions,to
for moves, andinto
for copies. This is nice in that we try to push users to use our no-copy solutions, but this will make.to_str
a little more ugly.as
for conversions,to
for copies, andinto
for moves. This keeps.to_str
pretty, but people might tend to the copy functions instead of the moves.as
into a user-implementable Cast trait #7080. This won't help when we have options for how to convert. For example,from_bytes_slice<'a>(vector: &'a [u8]) -> &'a str
which handles byte slices that don't end with a null character, orstr::from_bytes_with_null<'a>(vv: &'a [u8]) -> &'a str
which does.Anyone have an opinion on what we should use?
The text was updated successfully, but these errors were encountered: