The working Mechanism of match in Rust language

Rust is a novice rookie. I began to learn this young language just a few days ago, and I have been learning it for almost half a week.
I have to say that there are many things I don"t quite understand about the syntax of Rust, and the threshold for entry is higher than that of other languages.

such as this question.

Code 1:

let mut num1 = 1000;
let mut num2 = &mut num1;
println!("{}",num2);
*num2 += 100;
println!("{}",num2);

the output is as follows:

1000
1100

as expected, num2 is a pointer to num1.

Code 2:

let mut op = Some(100);
match op {
    None => {}
    Some(mut x) => {
        let mut change = &mut x;
        *change += 1000;
        println!("{:?}",*change);
    }
}
println!("{:?}",op);

according to my understanding of match, I think the

here
 Some(mut x) => {....}

is equivalent to

 let mut x = ....
The type of

x should be a & mut i32
then the type of change should be & mut & mut i32 = > & mut i32
and the output is

.
1100
Some(100)

Why can the change on the inside be changed, but the op on the outside remain the same? shouldn"t they be the same address?

what does the match keyword do with the following variables if it is not the same address?

welcome to discuss.

Jan.02,2022

let mut op = Some(100);
match op {
    None => {}
    Some(ref mut x) => {
        let mut change = x;
        *change += 1000;
        println!("{:?}",*change);
    }
}
println!("{:?}",op);

change to this, and the output is

1100
Some(1100)

now let's test whether a copy has occurred

struct Data {
    dat: i32
}

impl std::clone::Clone for Data {
    fn clone(&self) -> Data{
        println!("object cloned");
        Data {dat: self.dat}
    }
}

impl std::fmt::Debug for Data {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{:?}", self.dat)
    }
}

fn main() {
    let mut op = Some(Data {dat: 100});
    match op {
        None => {}
        Some(mut x) => {
            let mut change = &mut x;
            (*change).dat += 1000;
            println!("{:?}",change);
        }
    }
    println!("{:?}",op);
}

compile, error

error[E0382]: use of partially moved value: `op`
  --> src\main.rs:28:21
   |
22 |         Some(mut x) => {
   |              ----- value moved here
...
28 |     println!("{:?}",op);
   |                     ^^ value used here after move
   |
   = note: move occurs because the value has type `Data`, which does not implement the `Copy` trait

as you can see, match takes ownership of op, so op is invalid. As an i32 type, op is not invalidated, so a copy of
occurs and the pointer to the original variable is obtained using ref, so there is no transfer of ownership

Menu