Content-Length: 24734 | pFad | https://doc.rust-lang.org/std/pin/../hash/../../std/sync/../../../std/../error_codes/./E0508.html

E0508 - Error codes index

Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Error code E0508

A value was moved out of a non-copy fixed-size array.

Erroneous code example:

struct NonCopy;

fn main() {
    let array = [NonCopy; 1];
    let _value = array[0]; // error: cannot move out of type `[NonCopy; 1]`,
                           //        a non-copy fixed-size array
}

The first element was moved out of the array, but this is not possible because NonCopy does not implement the Copy trait.

Consider borrowing the element instead of moving it:

struct NonCopy;

fn main() {
    let array = [NonCopy; 1];
    let _value = &array[0]; // Borrowing is allowed, unlike moving.
}

Alternatively, if your type implements Clone and you need to own the value, consider borrowing and then cloning:

#[derive(Clone)]
struct NonCopy;

fn main() {
    let array = [NonCopy; 1];
    // Now you can clone the array element.
    let _value = array[0].clone();
}

If you really want to move the value out, you can use a destructuring array pattern to move it:

struct NonCopy;

fn main() {
    let array = [NonCopy; 1];
    // Destructuring the array
    let [_value] = array;
}








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://doc.rust-lang.org/std/pin/../hash/../../std/sync/../../../std/../error_codes/./E0508.html

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy