What is Move?
In Rust, a move transfers the ownership of a value from one variable to another. After a value is moved, the original variable becomes invalid, and trying to access it results in a compile-time error. This design enforces memory safety and prevents data races without relying on a garbage collector.
How Does Move Work?
When one variable is assigned to another, Rust transfers ownership of the data instead of making a copy. This is particularly important for types that allocate data on the heap, such asString or Vector, where shallow or deep copying would be less efficient.

Moving with Integer Types
For types that implement theCopy trait, like integers, Rust performs a simple bitwise copy. Both variables remain usable because integers are stored on the stack and the duplication is inexpensive.
Consider the following example:
Copy trait, the value is duplicated, leaving both x and y valid.
Understanding Traits and the Copy Trait
In Rust, a trait defines shared behavior across different types, functioning similarly to interfaces in other programming languages. TheCopy trait allows values to be duplicated rather than moved. For instance, since integers implement the Copy trait, assigning one integer to another does not invalidate the original variable.
Later in this article, we will discuss traits in greater detail. For now, understand that the
Copy trait enables efficient duplication of values on the stack.Moving with String Types
For string types, data is moved instead of copied. When you transfer ownership of a string, the original variable is invalidated to ensure that only one variable owns the heap-allocated data at any time. Consider this example:s1 after the move causes a compile-time error because Rust enforces a single owner for heap-allocated data.
Rust Internals: How Moving Works with Strings
A RustString is a heap-allocated type that contains three components:
- A pointer to the heap-allocated buffer holding the string data.
- The current length of the string.
- The total capacity of the allocated buffer.

Why Move Instead of Copy?
Moving ownership for heap-allocated types likeString offers several advantages:
| Benefit | Explanation |
|---|---|
| Performance | Only a shallow copy of the pointer, length, and capacity is performed, avoiding the overhead of duplicating large buffers. |
| Memory Safety | Ensures that only one owner is responsible for deallocating the heap memory, preventing double-free errors and similar issues. |

Double-Free Errors and Rust’s Ownership Model
A double-free error occurs when the same memory is deallocated twice. In languages like C or C++, improper memory management can result in two pointers referencing the same memory, leading to such errors. For example, consider this C code:Rust’s ownership model guarantees that there is always a single owner for a heap-allocated resource, which inherently prevents double-free errors and other memory safety issues.
The Process of Moving a String
When a string is moved in Rust, the following process occurs:- A shallow copy of the string struct (with its pointer, length, and capacity) is created for the new variable.
- The original string is marked as invalid at compile time, ensuring it cannot be used further.
- The heap-allocated data is now exclusively owned by the new variable.
- When the new variable goes out of scope, Rust automatically calls
dropto deallocate the heap memory.
