News for hackers. Stuff that matters.
Re:A half-hour to learn Rust by nobleach on Saturday January 02, 2021 @12:14PM (#25614282)
Yes, you can clone all day long and just reuse variable names as if you were writing JavaScript, but what does that do to your memory allocation?
Re:A half-hour to learn Rust by gpm on Saturday January 02, 2021 @12:17PM (#25614329)
Most languages just clone all day long, it's not that bad, rust clones (like most languages) are just to the first reference counted pointer after all.
Re:A half-hour to learn Rust by the_duke on Saturday January 02, 2021 @12:52PM (#25614666)
Well, yes and no.

Eg cloning a string leads to an extra allocation and a memcopy.

If you want to get a similar performance profile to GC languages, you have to stick your types behind a `Rc<T>>/Arc<T>` or `Rc<RefCell<T>> / Arc<Mutex<T>>` if you need mutability.

But modern allocators hold up pretty well to a GC, which amortizes the allocations. The extra memcopying can be less detrimental than one might think.

Re:A half-hour to learn Rust by throwaway894345 on Saturday January 02, 2021 @04:11PM (#25616574)
“Yes and no” is too generous; most languages clone only very infrequently (basically only for primitives where there is no distinction between a deep and shallow copy). For complex types (objects and maps and lists) they pass references (sometimes “fat” references, but nevertheless, not a clone).
Re:A half-hour to learn Rust by throwaway894345 on Saturday January 02, 2021 @12:31PM (#25614439)
To be clear, JS doesn’t require cloning because it has a gc. Not sure what you’re getting at with reusing variables names...
Re:A half-hour to learn Rust by nobleach on Saturday January 02, 2021 @02:08PM (#25615423)
What I mean is, if one does:

  const obj1 = { a: 32, b: 42 };
  function foo(ref) { ref.a = 0; }

  foo(obj1);
  console.log(obj1);
in JavaScript, one is just using the variable name as a "holder" of some value. One doesn't have to designate that that variable is being passed by reference. If one wanted to actually copy that object, they'd have to devise a mechanism to do so. In Rust, if someone doesn't specify, using the & symbol, that something is a reference, it'll end up moving the value.

Basically all I was saying is one can not approach writing Rust with a Java/JavaScript mindset. (That a variable is just a bucket holding a value). Care needs to be taken when referencing a variable as it may need to be moved, copied/cloned or referenced. In the case of copying, another memory allocation is done. So if someone approaches Rust from the standpoint of "this word represents a value, and I'm going to use it all over the place", they can find themselves blindly allocating memory.

Re:A half-hour to learn Rust by throwaway894345 on Saturday January 02, 2021 @04:15PM (#25616611)
Oh, by “reuse variable names”, you simply mean that values aren’t moved, i.e., that JS lacks move semantics or an affine type system. That’s a bit different to “reusing variable names”, which you can do in Rust as well provided the variable holds a (certain kind of?) reference.