yegor256
yegor256

Reputation: 105043

How to de-reference an integer element from enum reference?

This is my code:

enum Foo {
  X(i32)
}
fn take(foo: &Foo) -> i32 {
  match foo {
    X(a) => return a
  }
}

I'm getting:

6 | X(a) => return a,
  |                ^^^^ expected `i32`, found `&i32`

What is the right way?

Upvotes: 0

Views: 61

Answers (1)

Leo Aso
Leo Aso

Reputation: 12463

Use the de-reference operator *.

enum Foo {
  X(i32)
}
fn take(foo: &Foo) -> i32 {
  match foo {
    X(a) => return *a
  }
}

Upvotes: 1

Related Questions