Understanding Rust Error E0382: Ownership and Moves

Introduction to Rust Error E0382

Error E0382 in Rust occurs when a variable is used after its contents have been moved elsewhere. This is related to Rust's ownership system, which is designed to ensure memory safety by preventing data from being owned by more than one variable. In this article, we'll explore the different ways to handle this error and show you how to work around it by using references, cloning, and other techniques.

The Erroneous Example

Here's an example of Rust code that produces Error E0382:

struct MyStruct { s: u32 }

fn main() {
    let mut x = MyStruct{ s: 5u32 };
    let y = x;
    x.s = 6;
    println!("{}", x.s);

Using References

One way to avoid Error E0382 is by using a reference. When you use a reference, you are borrowing the value without changing its ownership. This allows you to pass a value to a function without moving it. Here's an example of using a reference:

fn main() {
    let s1 = String::from("hello");

    let len = calculate_length(&s1);

    println!("The length of '{}' is {}.", s1, len);

fn calculate_length(s: &String) -> usize {

Cloning Values

Another solution to Error E0382 is to create a duplicate of the value using .clone(). This makes a copy of the original value, allowing you to modify the clone without affecting the original. Here's an example of using .clone() to create a duplicate:

fn main() {
    let mut s1 = String::from("many");
    let s2 = s1.clone();
    println!("{} {}", s1, s2);

Marking Types as Copy

For certain types, such as integers, copying the value is straightforward and has no ownership implications. You can mark such types as Copy to allow implicit cloning. This way, you don't have to call .clone() to duplicate the value. Here's an example of marking a type as Copy:

#[derive(Copy, Clone)]
struct Point { x: i32, y: i32 }

fn main() {
    let mut p1 = Point{ x: -1, y: 2 };
    let p2 = p1;
    p1.x = 1;
    println!("p1: {}, {}", p1.x, p1.y);
    println!("p2: {}, {}", p2.x, p2.y);

Using Rc and RefCell

If mutable shared ownership is required, you can use Rc and RefCell to prevent Error E0382. This allows multiple variables to share ownership of the data, safely and correctly managing access at runtime. The following example demonstrates this approach:

use std::cell::RefCell;
use std::rc::Rc;

struct MyStruct { s: u32 }

fn main() {
    let mut x = Rc::new(RefCell::new(MyStruct{ s: 5u32 }));
    let y = x.clone();
    x.borrow_mut().s = 6;
    println!("{}", x.borrow().s);