Match and consume enum in Rust two times does not work

Horowitzathome

how can I match the value of an enum two times?

The problems seems to occur, if the value of an enum is "consumed" in a match. I don't understand why I get the error message "use of moved value ... value used here after move" -- see code below

I would understand to get the error if I return just the value but I am returning a clone of value and still get the error.

// Here a simple enum
enum SomeEnum {
    X(String),
}

// Then later in some function ...

// Test Enum which works
let x = SomeEnum::X(String::from(("a")));

let x_val1 = match x {
    SomeEnum::X(_) => 1
};
println!("x_val1 = {}", x_val1);

let x_val2 = match x {
    SomeEnum::X(_) => 1
};
println!("x_val2 = {}", x_val2);

// Test Enum which does not work
let y = SomeEnum::X(String::from(("b")));

let y_val1 = match y {
    SomeEnum::X(value) => value.clone()
};
println!("y_val1 = {}", y_val1);

// Does not compile, here I get error message ...
// use of moved value
//
// value used here after move
let y_val2 = match y {
    SomeEnum::X(value) => value.clone()
};
println!("y_val2 = {}", y_val2);
peterulb

By default, match statements consume all they can, so the value will be moved and owned. The compiler already suggests a workaround:

help: borrow this field in the pattern to avoid moving y.0
SomeEnum::X(ref value) => value.clone(),

See keyword ref:

Using the ref keyword, the value is only borrowed, not moved, making it available for use after the match statement:

What you currently do is letting the match take ownership and then clone. But at that point, ownership is already "gone". So the compiler complains about it.

But you can have it even simple in today’s Rust. If you match on a borrowed value match &y, then all bindings will attempt to borrow as well. This was introduced with match ergonomics.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Rust enum and match : How does it distinguishes two different enums that takes in two different struct but same contents

How does niche optimization for an enum work in Rust?

Rename enum fields in match (rust)

Using `match` together with a nested `enum` in Rust

Rust match mutable enum reference with vectors

Is there a way to match on all variants of an enum in rust?

Why does this regular expression to match two consecutive words not work?

Why does Rust use two bytes to represent this enum when only one is necessary?

Why does Rust have struct and enum?

How does Rust store enum values in arrays?

Is it possible to consume a typescript interface that does not fully match the data

Query does not seem to work with enum

How does enum in Swift work?

Why does not 'while(csv.Read())' work two / three times in CsvHelper?

Rust: Match statement mismatched type error for enum Result

How does the acker work if I consume twice from w bolt?

Why does't Rust match an empty array?

Rust macro does not match passed types

Why does Match compile with invalid Type in Rust?

How to check if enum does not match a pattern?

Object does not match target type with SetValue and enum

'if not ' does not work with two or three 'and'

preg_match does not work

Search if property is match does not work

getElementsByTagName() applying two times don't work

Why does this rand use statement work in rust?

how does rust move semantics actually work

Rust ? operator does not work with Into<T> trait

Example from Rust in Action does not work