I'm really sorry for my way of questioning if it's confusing u, new to rust
let p = vec![23, 3, 31, 4];
let mut p1 = p;
p1.push(10);
let p2 = &mut p1;
p2.push(11);
The above code ownership is moved from p to p1, And to push a value to vec p1 should be a mut variable but when p1 is mut referred to p2 then p2 doesn't need to declared as mut and push on p2 still works.
My doubt comes from when p owner ship is moved, only the data which p holds on heap is now pointed to stack of p1 and p is not valid -> in this case case only data is moved not the property of p as immutable vector is not moved as p1 has it's how variable property (mut)
sameway when p2 is mut referred p1 , and without explicitly mentioning it's mut on variable side as p2, p2 is able to edit as it is borrowing mutable reference
In p1 = p , p1 needs to be explicitly mention as mut , because p is immut
Then same way p2 also must be mentioned as mut ?
Ownership transfer creates a new binding, and the new owner needs to declare how it will treat the data (mutable or immutable).
Borrowing does not create a new binding. Instead, it temporarily shares the existing ownership. The mutability of the borrow depends entirely on the type of borrow (& or &mut).
so it's simple as if ownership is moved new owner can decided whether it's mut or immutable as he is owner
when it's borrowed the borrower can do only what permission the owner gives , because he is not the owner so he/she does not have rights , simple as it is :)
in below code even p1 explicitly declared it's mut but it's give error as it's only referred.
let p = vec![23, 3, 31, 4];
let mut p1 = &p;
p1.push(10);
// cannot borrow `*p1` as mutable, as it is behind a `&` reference `p1` is a `&` reference, so the data it refers to cannot be borrowed as mutable