1

Let's say I have such a function:

fn take_by_ref(i:&i32){
    println!("{}",i);
}

Now I want to pass mutable reference there:


#[test]
fn test_mut(){
    let mut x = 9;
    let m = &mut x;
}

And it is unclear what is the difference between the two calls to this function?

first:

take_by_ref(m); 

second:

take_by_ref(&*m); 

Are these two calls equal and the compiler makes first call as the second one automatically? or is there a difference? And won't the rule that either mut reference or immutable can exist be violated here when call take_by_ref?

That is, the question is that when passing a mut variable to such a function, it can become auto-immutable?

Alpharius
  • 489
  • 5
  • 12
  • Or maybe this one? [What is the relation between auto dereferencing and deref coercion?](https://stackoverflow.com/questions/53341819/what-is-the-relation-between-auto-dereferencing-and-deref-coercion) – cafce25 Jan 12 '23 at 14:27
  • @cafce25 Where do you see auto-dereferencing here? – interjay Jan 12 '23 at 14:30
  • TL;DR - yes, `take_by_ref(m)` is semantically the same as `take_by_ref(&*m)` due to deref coercion. – trent Jan 12 '23 at 14:52

0 Answers0