Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upRFC: standardizing on naming functions that convert from one type to another #7087
Comments
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
|
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.
|
This comment has been minimized.
This comment has been minimized.
|
nominating |
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
|
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) |
This comment has been minimized.
This comment has been minimized.
|
accepted for backwards-compatible |
erickt
referenced this issue
Sep 12, 2013
Closed
Decide on a naming convention for casting methods. #7151
This comment has been minimized.
This comment has been minimized.
|
Another bikeshed option. We could rename methods like |
This comment has been minimized.
This comment has been minimized.
|
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:
|
This comment has been minimized.
This comment has been minimized.
|
One more bikeshed option: |
This comment has been minimized.
This comment has been minimized.
|
We should probably also decide on something for (e.g.) Currently we're just using |
This comment has been minimized.
This comment has been minimized.
|
@huonw I don't think that should be relevant. it makes an copy, whether that's actually expensive depends on the type. |
This comment has been minimized.
This comment has been minimized.
|
This was opened a year ago and been last updated more then 6 month ago, is it still outstanding? |
brson
assigned
aturon
Aug 1, 2014
This comment has been minimized.
This comment has been minimized.
|
The guidelines have now made Closing as resolved. |
erickt commentedJun 12, 2013
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
strmod: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:
asfor conversions, andtofor moves or copies. Simple, but then we end up with functions named liketo_str_consumeto distinguish between the two cases, which is a little ugly.asfor conversions,tofor moves, andintofor copies. This is nice in that we try to push users to use our no-copy solutions, but this will make.to_stra little more ugly.asfor conversions,tofor copies, andintofor moves. This keeps.to_strpretty, but people might tend to the copy functions instead of the moves.from_bytes_slice<'a>(vector: &'a [u8]) -> &'a strwhich handles byte slices that don't end with a null character, orstr::from_bytes_with_null<'a>(vv: &'a [u8]) -> &'a strwhich does.Anyone have an opinion on what we should use?