lotsoftools

Understanding Rust Error E0070: Assignment Operator on Non-Place Expression

Introduction to Rust Error E0070

Rust Error E0070 occurs when an assignment operator is used on a non-place expression. This error indicates that the left-hand side of an assignment operation must be a memory location, also known as a place expression.

Invalid Usage of Assignment Operator

The following code examples demonstrate erroneous uses of assignment operators:

struct SomeStruct {
    x: i32,
    y: i32,
}

const SOME_CONST: i32 = 12;

fn some_other_func() {}

fn some_function() {
    SOME_CONST = 14; // error: a constant value cannot be changed!
    1 = 3; // error: 1 isn't a valid place!
    some_other_func() = 4; // error: we cannot assign value to a function!
    SomeStruct::x = 12; // error: SomeStruct a structure name but it is used
                        //        like a variable!
}

Valid Usage of Assignment Operator

To resolve Rust Error E0070, ensure that the left-hand side of the assignment operator is a place expression. The following working examples demonstrate proper use of assignment operators:

struct SomeStruct {
    x: i32,
    y: i32,
}
let mut s = SomeStruct { x: 0, y: 0 };

s.x = 3; // that's good !

// ...

fn some_func(x: &mut i32) {
    *x = 12; // that's good !
}

Rules for Place Expressions

In Rust, place expressions represent memory locations. They can be a variable with optional namespacing, a dereference, an indexing expression, or a field reference. Detailed information about place expressions can be found in the Expressions section of the Rust reference.