Search code examples
pointersrustref

Why do I have to use ref when matching on a dereferenced enum?


Why do I have to use ref s and not only s in destruct method of impl Keys?

#[derive(Debug)]
enum Direction {
    Up(Point),
    Down(Point),
    Right(Point),
    Left(Point),
}

#[derive(Debug)]
struct Point {
    x: u32,
    y: u32,
}

#[derive(Debug)]
enum Keys {
    Up_key(String),
    Down_key(String),
    Right_key(String),
    Left_key(String),
}

impl Direction {
    fn match_direction(&self) -> Keys {
        match *self {
            Direction::Up(_) => Keys::Up_key(String::from("Up key is pressed")),
            Direction::Down(_) => Keys::Down_key(String::from("Down key is pressed")),
            Direction::Right(_) => Keys::Right_key(String::from("Right key is pressed")),
            Direction::Left(_) => Keys::Left_key(String::from("Left key is pressed")),
        }
    }
}

impl Keys {
    fn destruct(&self) -> &String {
        match *self {
            Keys::Up_key(ref s) => s,
            Keys::Down_key(ref s) => s,
            Keys::Left_key(ref s) => s,
            Keys::Right_key(ref s) => s,
        }
    }
}

fn main() {
    let test_1 = Direction::Right(Point { x: 1, y: 0 });
    let x = test_1.match_direction();
    println!("{:#?}", x);
    let k = x.destruct();
    println!("{}", k);
}

Output:

Right_key(
    "Right key is pressed",
)
Right key is pressed

Solution

  • Using ref prevents the pattern matching from taking ownership of s.