3 ms·
It’s weird to me the degree to which you’re pressing an offhand comment into being some kind of Statement. I’d expect anyone writing Rust regularly to know tha
by mplanchard 4y ago
It’s weird to me the degree to which you’re pressing an offhand comment into being some kind of Statement.
I’d expect anyone writing Rust regularly to know that the impact of Clone is context-dependent, and whether or not you should be fine with using it liberally is also context-dependent. Big object? Hot loop? Avoid cloning. Not worried about performance? Clone all you want if it makes the code easier and faster to write. You can always optimize later.
Sometimes it’s easy to avoid clones, and if you’re writing a lot of Rust it often feels like you might as well, just in case. But at least for my team, “you could get rid of this clone” is generally a take-it-or-leave-it kind of comment in code review.
There’s no easy way to express “it’s a trade-off between performance and ease of use that you’ll get better at with time” in a way that is instructive in any meaningful way, which is why I imagine it’s more useful to push people to err on the side of what’s going to be faster to get them to the amount of experience they need to engage meaningfully with the question within their own context.
- tptacek 4y agoWell, if I can mostly ignore fussy ownership and borrowing stuff and just clone everything, all the time, getting rid of clones exclusively in places that I profile and learn to be in the hot path, and the resulting code will be idiomatic Rust, that would be welcome news to me. That's where I'm coming from. My impression from Klabnik's most recent comment is that this isn't the case. Which is fine! It lines up with my previous assumptions about Rust idiom.
- steveklabnik 4y ago> My impression from Klabnik's most recent comment is that this isn't the case. Okay last comment in this sub-thread because I have to get back to my job here, which hilariously is adding some boxing and possibly cloning in order to improve a codebase. I feel like I'm going crazy. I feel like this: > if I can mostly ignore fussy ownership and borrowing stuff and just clone everything, all the time, getting rid of clones exclusively in places that I profile and learn to be in the hot path, and the resulting code will be idiomatic Rust is exactly what my last comment said. This whole chain, in my experience, is me saying "clone is not a moral issue" and you saying "why are you saying that people that use clone should feel bad." So I need to bow out. I wish I could figure out where this disconnect is, but it's clear that this thread is not achieving that.
- lcall 4y agoYour comments and much of this thread have been helpful to my understanding. (Like the Rust book!--thank you!). I also appreciate your effort to be kind/respectful/understanding, elevating the conversation, where others might have responded with anger or disparagement. Thanks for the example.
- Serow225 4y ago<3
- burntsushi 4y agoI'm not quite sure where the disconnect is either, but yeah, "should I clone everywhere" or "is cloning everywhere idiomatic" is just one of those models that is useful, but like all models, is invariably wrong in some cases. Just as one example, consider a function I wrote. It accepts a &str as a parameter. Internally, among other things, it clones that &str into a String. If you asked me whether that was "idiomatic" devoid of any other context, I would say, "probably not? Why not just ask for the String or a Into<String> instead?" But it's not a certainty. Just a likelihood. But now let's add context. The context is that that function I mentioned is Regex::new. Does that change my answer? You bet your ass it does. Because cloning that pattern string has now turned into a definitively negligible cost. (By many orders of magnitude.) And there is upside to accepting a &str: it's non-generic, a little easier to understand and is unlikely to ever run afoul of type inference. Those aren't huge upsides, but there is actually no downside here because of the context. As others have pointed out, there is no one-size-fits-all answer to this question. There are models though, and those models (e.g., "don't worry about cloning while you're learning Rust") are very likely to serve you quite well. That might not be every path that everyone learning Rust always takes. But it's a decent bet in my experience.
- tptacek 4y agoThis makes sense! (Also, even if I was deliberately cloning liberally, I'd still pass &str to most things).