3 ms·
We put this aside in the book for this exact reason: https://doc.rust-lang.org/stable/book/ch12-03-improving-error-handling-and-modularity.html?#the-trade-offs-
by steveklabnik 4y ago
We put this aside in the book for this exact reason: https://doc.rust-lang.org/stable/book/ch12-03-improving-error-handling-and-modularity.html?#the-trade-offs-of-using-clone https://doc.rust-lang.org/stable/book/ch12-03-improving-erro...
> But I felt like if I could have worked out borrowing it would've generated more efficient code and less memory consumption.
That is possible! But while you're learning, this isn't the most important thing to focus on. And depending on what you're trying to do, using borrowing may be easy, or may be hard, and while you're trying to learn, you don't know enough to evaluate the tradeoff on that basis, so in my humble opinion, it is absolutely okay to clone, and is probably even what you should do in most cases.
Once you're feeling good enough about Rust to start digging deeper, working on trying to not do this is a valuable skill. But imho it's a waste of time when you're starting, and may lead you into a pit of despair.
- tptacek 4y agoThe aside in the book says the opposite of what you claim it to be saying. Here, you suggest that liberal cloning is the way it should be; there, you say it's fine for beginner code, which is a very unsubtle nudge. Which is it?
- withoutboats3 4y agoI think the confusion comes from the fact that Steve would consider code written by someone who "hasn't written much Rust" to be "beginner code," so he meant exactly what the book says. I would agree that people getting the hang of Rust shouldn't worry too much about this, but that production code that deep clones buffers when it could use references is badly written (then again, it may not matter at all!).
- steveklabnik 4y agoI am a little offput by how aggro this comment is, but I really like you and so will give you the benefit of the doubt here. I wrote this text years ago. It is possible I didn't do as good of a job as I could have. This was added directly in response to situations' like the OPs, people saying that they felt bad using clone while they were learning Rust, and so the comment, coming early in the book, and as a text for learners, focuses on that case. It's also the same as in this thread, I thought I was being pretty clear that I'm talking about people learning Rust? However, just because I say one thing doesn't mean I'm implying something else; for non-beginners, I don't think clone is a moral issue either way. If you are trying to get ultimate performance, clone may be an issue. It also may not. Some clones are more expensive than others. Furthermore, not everyone needs to get ultimate performance, and that's okay too. It is up to you. The point is that I don't think this question is a useful one for someone to engage with as a beginner. It is a useful question for intermediate programmers to engage with, but so that they can make the right call for them, not because one or the other is the correct answer in all situations.
- tptacek 4y agoAs a Rust programmer, I have the exact same lived experience of the language that the parent commenter does: that Rust can be relatively straightforward to write if you liberally clone things, but that the prevailing community norm strongly suggests that cloning is a code smell, or something you do in tooling-grade code. The parent commenter was surprised that you suggesting liberal cloning was the way Rust code should be --- that, like, the median Rust crate should probably be written in the "clone liberally" register. You said that was the reason you wrote the Rust book aside. But I read that book and that aside and reached the opposite conclusion, and I think people who follow your link will mostly see why. You haven't exactly cleared things up here, either. "For non-beginners, clone isn't a moral issue". Well, that's not what you just said upthread. I'm pressing you on this because it would be helpful to get an actual clear signal on this. Should most code be written with liberal clones, or should most beginner code be written with liberal clones? Like the previous commenter, I just want to understand what you're trying to say. (A sibling comment here says the exact thing that I'm saying creates the mixed message here: that cloning is fine... in beginner code, but not in serious code.) This is not, as we both know, a minor distinction in Rust; it is maybe the biggest stylistic distinction in actually writing Rust? Moreso maybe even than async?
- steveklabnik 4y ago> that, like, the median Rust crate should probably be written in the "clone liberally" register. That is not what I understand myself to have said. The parent said that they "do not write much Rust code." That is very different than "the median Rust crate." > Should most code be written with liberal clones, I think this question is a category error. There is not enough context to give an opinion. And I think that means you cannot make a broad generalization about clone; some code should have a bunch of clones, and some code should not, even if they're the same code, because they operate in different kinds of contexts or sure, registers, to sort of stick to the vibes of the original post. If I am presented the exact same code by two people, and one of them started writing Rust yesterday, and says "what do you think of my code?" I would not even give consideration to the number of clones when attempting to give them my opinion about their code. If someone came to me after writing Rust for ten years and said "hey I am struggling to improve performance in this code" and said "what do you think of my code?" and I saw a bunch of clones, I would say "have you profiled? I would suspect that maybe clones are causing your problem but I wouldn't do anything until I had a flame graph or equivalent." Even then that is not a moral question or answer, it is an engineering one. > or should most beginner code be written with liberal clones? Even this isn't exactly what I meant, but is closer. I mean what I said in my parent comment to you, beginners should not feel bad if their code uses clone, whatsoever. That does not then imply that non-beginners should feel bad, I said absolutely nothing in this thread about anything other than beginner code (until the paragraph above in this comment and the paragraph in the comment you're currently replying to).
- Serow225 4y agomy interpretation of the “clone is the way to go” comment is that it’s in the context of the OP saying “I haven't written much Rust…”, which puts a pretty clear frame around the intended applicability of the statement no? I don’t taking it at all as being an absolute statement.
- biorach 4y ago> The aside in the book says the opposite of what you claim it to be saying. Here, you suggest that liberal cloning is the way it should be; there, you say it's fine for beginner code, which is a very unsubtle nudge. Which is it? That's phrased way too harshly, considering you're talking to someone who has put significant efforts into making Rust accessible to others.
- zozbot234 4y ago"Beginner code" is a bit misstated, yes. An abundance of .clone()s does suggest that the code path might be unoptimized, but unoptimized code isn't solely written by beginners. If you want to use Rust for quick prototyping, you're going to use .clone() a lot. It's a bit regrettable that the book doesn't clarify this more.