Questions about `&mut T` and move semantics, `&mut T` is "move-only"?

Correct. An example of where coercions come into play would be if the function took &mut [i32], and you had a &mut Vec<i32>:

fn f(zs: &mut [i32]) {
    for z in zs {
        *z += 1;
        println!("{}", *z);
    }
}

fn main() {
    let mut x: Vec<i32> = vec![5, 6];
    let y = &mut x;
    
    f(y);               // implicit reborrow and coercion
    f(&mut *y);         // explicit reborrow, implicit coercion
    f(y.as_mut_slice()); // explicit reborrow and conversion

    y[0] += 1;
    println!("{}", y[0]); // '8'
    x[0] += 1;
    println!("{}", x[0]);  // '9'
}
3 Likes