1

I am learning Rust and I am having a little difficulty with references and related, and I didn't got one thing:

fn main() {
    let number = &mut 0;

    println!("Ref: {}, Value: {}", number, *number);

    *number += 1;

    println!("Ref: {}, Value: {}", number, *number);
}

The output is 0 for both ref and value in the first println! and 1 for both ref and value in the second println!, wich doesn't make sense, as I added one to the referenced value, not the reference itself. Indeed, trying to add any value to the reference itself yields an error. Why number and *number are returning the same value?

Mateus Felipe
  • 906
  • 1
  • 12
  • 36

0 Answers0