What are move semantics in Rust?

Have you ever wondered how Rust prevents memory leaks? Or why Rust code is often so efficient? The answer lies in move semantics.

In Rust, every value has a unique owner at any given time, and the owner is responsible for freeing the associated memory when the value is no longer needed. Move semantics is a way of transferring ownership of a value from one variable to another. This is done by copying the data of the value to the new variable and then destroying the old variable.

Moving a function argument

Let's say we have the following code:

fn foo(s: String) {
// s is moved into foo, and s is no longer valid
println!("{}", s);
}
fn main() {
let s = String::from("Hello, world!");
foo(s);
}

Explanation

In the code above, ownership of the String value s is transferred from the main function to the foo function using move semantics. As a result, s in the main function become invalid or unusable.

Moving a struct

Let's say we have the following code:

struct Point {
x: i32,
y: i32,
}
fn main() {
let p1 = Point { x: 10, y: 20 };
let p2 = p1;
// p1 is moved to p2, and p1 is no longer valid
println!("({}, {})", p2.x, p2.y);
}

Explanation

In this example, the struct Point is first created and initialized with the values 10 and 20. Then, the value of p1 is moved to p2. This means that the data of p1 is copied into p2, and p1 is then destroyed.

Moving a string

Let's say we have the following code:

fn main() {
let s1 = String::from("Hello, world!");
let s2 = s1;
// s1 is moved to s2, and s1 is no longer valid
println!("{}", s2); // Prints "Hello, world!"
}

Explanation

The ownership of the string value "Hello, world!" is moved from the s1 variable to the s2. After the move, the s1 variable becomes invalid, and attempting to use it would result in a compile-time error as shown below:

Advantages

Move semantics in Rust offers several benefits, including:

  • Prevention of memory leaks

  • Prevention of use-after-move bugs

  • Efficient resource management

  • Compiler-verified memory safety

  • Clear ownership semantics

  • Facilitation of automatic resource deallocation

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved