hewc
hewc

Reputation: 129

What does "match ref" mean in rust?

fn main () {

   let x: &Option<_> = &Some(90);

    match x {
        // no ref
        &Some(y) => { 
            print!("{}", y);
        },
        &None => { 
        
        },
    }
    
    match x {
        // ref
        &Some(ref y) => { 
            print!("{}", y);
        },
        &None => { 
        
        },
    }
    
}

// What's the difference between the two?

Upvotes: 0

Views: 679

Answers (1)

Hauleth
Hauleth

Reputation: 23556

ref is inverse of & on the left side of match. In other words:

let ref a = x;

Is the same as

let a = &x;

The reason for that syntax is that with structural matching it is not always possible to use 2nd form:

struct Foo(usize);

let Foo(ref a) = x;

Upvotes: 4

Related Questions