This is the 23rd day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Ownership rule

Ownership has a set of rules. Let’s look at the specific rules of ownership:

-Each value has a variable that is the owner of the value.-Each value has one and only one owner at any time-When the owner (variable) leaves its scope, the value is removedCopy the code

Variable scope

The ownership rule mentioned above says that when the owner (variable) leaves its scope, the value is deleted.

Scope is the valid scope of an item in a program. For example, in the following code, the variable I binds a string literal that is valid from the declared point until the end of the current scope.

fn main() {
    // I is invalid when not declared
    let i = "rust";  // From here on, I is valid
    
    println!("{}", i); // We can operate on the I variable
}
// the scope of I ends here, and I is no longer valid
Copy the code

In summary, the above code has two important event points:

  • wheni Go into scopeWhen, it is valid.
  • This continues until it leaves scope.

So far, the relationship between valid variables and scope is similar to that of other programming languages. Now let’s build on that and introduce String.

Type string

The string type is a more complex data type than even the basic scalar types described in the previous article, which are moved off the stack when stored, so a heap of data is needed to explore how Rust knows when to clean up data. The string type is the data stored on the heap.

However, the focus here is on strings and the proprietary parts, which are equally applicable to other complex data types provided by the standard library or created by the library. Strings will be covered in more detail in a later article.

String literals are convenient, but not suitable for every scenario where text is used. One reason is that they are immutable. Another reason is that not all string values are known at code time: what if, for example, you want to take user input and store it? For this reason, Rust has a second String type, String. This type is assigned to the heap, so it can store text of unknown size at compile time. You can use the FROM function to create a String based on a String literal as follows:

fn main() {
    let s = String::from("rust");
}
Copy the code

These two colons (::) are operators that allow specific FROM functions to be placed in a namespace of type String without the need for a name like string_FROM.

We can also modify such strings:

fn main() {
    let s = String::from("rust");
    s.push_str(", good");  // the push_str method appends the value to the string
    println!("{}", s);  // The result is rust, good
}
Copy the code

This raises the question, why can strings be mutable and literals not? The difference lies in how the two data types handle memory, which will be explained in the next article

conclusion

The article was first published in the wechat public account Program Yuan Xiaozhuang, at the same time in nuggets.

The code word is not easy, reprint please explain the source, pass by the little friends of the lovely little finger point like and then go (╹▽╹)