6 ms·
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 f
by tptacek 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?
- 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 agoThere is one tangential thing that I’ve thought about before—it would be nice if there was some way to encode this into the library ecosystem (ie crates.io) I have no idea how this would be realistically possible though—it’s pretty clear that you can rely on categories that are statically verifiable like “no-std/no-alloc”. But what mechanism could be used to allow library consumers to distinguish between things like “laser focus on high perf/low-alloc” vs “prioritizes API over perf-at-all-costs” vs “this package is an experiment/Baby’s First Crate/shitpost” ?? They’re all valid choices for different consumers or usecases.
- steveklabnik 4y agoYeah, there is something in this space for sure. I think one of the issues is time; code can start off as a fun experiment, but transform into one that's not, which maybe just means that it is metadata associated with a specific release, yadda yadda, but there's a lot here to think about. For sure. I recently have been accepting some PRs to some Ruby code that I wrote ten years ago that ended up being still in-use today. "Shitpost" or "baby's first crate" isn't accurate, but "hey I was very serious about this before but now it's not as high priority for me but that doesn't mean I abandoned it" is a tough thing to encode.
- evntdrvn 4y agoAbsolutely true re evolution. That's why it would have to probably be crate keywords rather than categories, I'm thinking. This whole space about encoding library non-technical aspects like intent and status seems like it has a lot of possibilities. I wonder if there's prior art in other language ecosystems?
- lcall 4y agoI replied just below, to another comment, with an idea. Not fleshed out well, but oh well.
- steveklabnik 4y agoPerl has the Acme namespace for expressly silly projects, that’s the first thing that comes to mind…
- 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.