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

This paper mainly introduces the relationship between the ownership of the Rust and function, we know that the value passed to a function on the semantic and similar to the variable assignment, like function transfer value may move or copy, as variables and the data of two kinds of interaction, through the code sample below for Rust to explain the relationship between ownership and function.

Ownership and function

Use the following code example to illustrate when a variable enters and leaves scope:

fn main() {
    let s = String::from("hello");  // The variable s goes into scope
    
    move_variable(s);  // the value of s is moved to move_variable
    					// Therefore, s is no longer valid in this case
    
    let x = 1;  // the variable x goes into scope
    
    makes_copy(x);  // x should be moved to function, but i32 is copy, so x can continue to be used in the scope of the x variable
    
} // here x is removed first, then s, but since the value of s has been removed, there is no operation on s

fn move_variable(string_str: String) {  // string_str enters scope
    println!("str is {}", string_str);
}  // string_str moves out of scope Rust calls the drop method to reclaim memory used by string_str

fn makes_copy(some_integer: i32) { // some_integer enters the scope
    println!("{}", some_integer);
} // here, some_INTEGER moves out of scope. There will be no special operations
Copy the code

When you try to use s after calling move_variable, Rust throws a compile-time error. These static checks keep us from making mistakes. Try adding code that uses s and x to the main function to see where we can use them and where ownership rules prevent us from doing so.

Function returns a value and scope

The return value of a function can also be transferred ownership, as illustrated by the following code:

fn main() {
    let s1 = gives_ownership();         // Gives_ownership will return a value
                                        / / move to s1

    let s2 = String::from("hello");     // s2 enters scope

    let s3 = takes_and_gives_back(s2);  // s2 is moved to
                                        / / takes_and_gives_back,
                                        // It also moves the return value to s3
} // here, S3 moves out of scope and is discarded. S2 is also out of scope, but has been removed,
  // So nothing happens. S1 moves out of scope and is discarded

fn gives_ownership() - >String {             // gives_ownership moves the return value to
                                             // Call its function

    let some_string = String::from("hello"); // some_string enters scope.

    some_string                              // Returns some_string and moves it out to the calling function
}

// Takes_and_gives_back will pass in the string and return the value
fn takes_and_gives_back(a_string: String) - >String { // a_string goes into scope

    a_string  // Returns a_string and moves it out to the calling function
}

Copy the code

Variable ownership always follows the same pattern: move a value when assigning it to another variable. When a variable holding data values in the heap leaves scope, its value is cleared by DROP, unless the data is moved to another variable.

Getting ownership in every function and then returning ownership is a bit verbose. What if we want the function to use a value without taking ownership? If we were to continue using it, it would be a little annoying to pass it in and return it every time, and we might also want to return some data generated in the body of the function. In this case we can use tuples to return multiple values:

fn main() {
    let s1 = String::from("hello");

    let (s2, len) = calculate_length(s1);

    println!("The length of '{}' is {}.", s2, len);
}

fn calculate_length(s: String) - > (String.usize) {
    let length = s.len(); Len () returns the length of the string

    (s, length)
}
Copy the code

It’s a bit formalistic in official terms, and it should be quite common. So Rust provides a feature called references for this.

References to this knowledge will be updated in future articles

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 (╹▽╹)