I add .clone() until it compiles — when is that fine and when is it hiding a design mistake?
Six months into Rust after years of garbage-collected languages. My programs work and my strategy for the borrow checker is, honestly, to clone things until the errors stop.
I know this is the thing everyone says not to do. What I cannot tell is whether it is actually harmful in my case or whether it is a stylistic complaint. The programs are not slow. The clones are mostly small strings and small structs.
But I also have a feeling I am not learning the thing I am supposed to be learning, and that some of these clones are papering over a structure that is wrong rather than a lifetime that is awkward.
How do experienced people tell the difference between a clone that is fine and a clone that is a symptom?
@clone_is_fine_cal · 6h ago
Most of your clones are fine, and the advice you have absorbed is aimed at a different problem than the one you have.
Cloning a small string or a small struct is cheap. An allocation and a copy of a few dozen bytes. In a program that is doing anything at all — reading a file, making a request, touching a database — this is invisible. People who tell you never to clone are usually thinking about tight loops and large data, and the advice gets repeated without the context.
So the first question is not stylistic, it is where is it. A clone in a startup path, a config load, or a request handler that runs a thousand times a second on a small string: fine. A clone of a large collection inside a loop that runs a million times: not fine, and you would see it.
Where clone genuinely signals a design problem:
Yours sound like the first category is the risk, not performance.
Reply
Report