5 ms·
Really? I felt like I was just punting because I didn't take time to understand and resolve potential dependencies. In my case the data was relatively small a
by wyldfire 4y ago
Really? I felt like I was just punting because I didn't take time to understand and resolve potential dependencies.
In my case the data was relatively small and I just wanted functionality. But I felt like if I could have worked out borrowing it would've generated more efficient code and less memory consumption.
- lordnacho 4y agoI'm undecided. I feel it's a bit like when you .unwrap() everything. Either I take the time to match all the potential results, or I do it quick and dirty. But also when it comes to cloning if you don't do it you get a lot of lifetime annotations.
- steveklabnik 4y agoWe 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?
- ripley12 4y agoPremature optimization, etc. etc. I tend to follow a "clone first, figure out lifetimes later if profiling shows that it would be worthwhile" rule. If I always did the optimal thing with lifetimes, I'd never actually finish anything.
- philosopher1234 4y agoIt begs the question, why is clone a method and borrowing implicit? Perhaps it should be the opposite. `.borrow` or something like that.
- colejohnson66 4y agoTechnically, it's not implicit; You need to prefix your value with `&` to borrow it.
- steveklabnik 4y agoBorrowing isn't implicit, as you have an &. But even beyond that, clone is not implicit because it may be an expensive operation, and so Rust wants to expose that to you. It avoids issues like this: https://news.ycombinator.com/item?id=8704318 https://news.ycombinator.com/item?id=8704318
- TheCycoONE 4y agoThe Copy trait is for cases where it's cheap enough to be implicit. Otherwise the philosophy of the designers was that things that may be expensive should be explicit.
- kaba0 4y agoOr even the reverse may be true — copying a block serially is insanely fast on modern processors.