Variable ownership type


It’s actually a question and answer post. The official answers too many questions and doesn’t want to answer them,
Look at the source code of the add method of string. See a piece of code that is a little confused. Record it.

The following is a simple example

struct B {

impl B {
  fn push(mut self, s:&str) ->Self {;

The understanding of variability has always been in the variable binding area. For example, there is a structure B, which creates a variable binding

 let mut x = B{name:String::from("abc")};

Create a variable x and bind it to the instance of structure B. x owns the ownership of this instance of structure B. structure B has a method push that can be called

let x = x.push("a");

There is no problem with this, and so is the understanding of previous ownership. But I thought about it carefully and called it in another way

let x = B{name:String::from("abc")}.push("ii");

That’s right. Normal compilation and execution revolve around a variable type. Don’t you need to specify mut? I don’t understand for the time being. If you understand, you can comment

After several implementations, I still feel that the reference and ownership are not fully understood. As long as the function uses the variable parameters of the ownership object, it only needs to transfer the instance. It doesn’t matter whether it is bound by variables (variable and non variable).
Variable binding is not a reference to this instance (regardless of memory storage), it should be regarded as this instance type
fn main() {
    let x1 = B{name:String::from(“abc”)};
    let mut x2 = B{name:String::from(“123”)};

fn   mut_ test(mut   s:    B)   {/ / mut removes compilation exceptions“abc”);
In fact, I still don’t understand the relationship between citation and ownership

This work adoptsCC agreement, reprint must indicate the author and the link to this article