Why do values need to be moved?

It's important to know that a String has two parts:

  1. The struct itself, which contains a pointer and two integers (length and capacity).
  2. The allocation on the heap, pointed at by the pointer.

Moving a String only moves the first part. The allocation is not moved, and this makes it cheap to move a String.

Of course, in your example, it is better to not move it, but sometimes the function actually needs ownership of the value, and in that case, you must move it.