Rust 所有权理解

本文深入探讨了Rust编程语言的核心特性——所有权系统。所有权规则确保了唯一所有权,当变量作用域结束时,值会被清理。栈上的基本类型在作用域结束时自动释放,而堆上的复杂类型如String需要手动管理。通过移动和克隆操作,Rust控制变量间的数据交互。移动会改变数据的所有权,而克隆会复制堆数据。所有权对于理解函数参数传递和内存安全至关重要。
摘要由CSDN通过智能技术生成

Understanding OwnerShip

1. What Is Ownership?

​ Rust’s central feature is ownership.

1.1 Why need it?

Rust uses a third approach: memory is managed through a system of ownership with a set of rules that the compiler checks at compile time.
It’s an approach to manage the way they use a computer’s memory while running.

Managing heap data is why ownership exists can help explain why it works the way it does.

1.2 Concept

Ownership Rules:

  • Each value in Rust has a variable that’s called its owner.
  • There can only be one owner at a time.
  • When the owner goes out of scope, the value will be dropped.

2. OwnerShip Explanation

2.1 Stack and Heap

  1. These types are all stored on the stack and popped off the stack when their scope is over.
  • Scalar Types: Integers, floating-point numbers, Booleans, and characters
  • Compound Types: Tuples and arrays
  1. Complex data types (such as String)

     {
            let s = String::from("hello"); // s is valid from this point forward
    
            // do stuff with s
     }                                  // this scope is now over, and s is no
          
    

    When a variable goes out of scope, Rust calls a special function for us. This function is called drop, and it’s where the author of String can put the code to return the memory.

    String Type is mutable, growable. So we need to allocate an amount of memory on the heap.

2.2 Ways Variables and Data Interact: Move

  • Integer (Allocated in Stack)

    let x = 5;
    let y = x;
    

    let y = x make a copy of the value in x and bind it to y, and these two 5 values are pushed onto the stack.

  • String (Allocated in Heap)

    Tips: String type is made up of three parts:

    This group of data is stored on the stack, but ptr is a pointer to the memory in heap that holds the contents of the string.

    image-20210708225525591

    let s1 = String::from("hello");
    let s2 = s1;
    
    println!("{}, world!", s1); //error
    // 'let s2 = s1' means rust considers s1 to no longer be valid 
    //  therefore, Rust doesn’t need to free anything when s1 goes out of scope.
    

    ​ When we assign s1 to s2, the String data is copied, meaning we copy the pointer, the length, and the capacity that are on the stack, but we do not copy the data on the heap that the pointer refers to.

    ​ In this example, we would say that s1 was moved into s2

    ​ The data representation in memory looks:

    image-20210708230030228

2.3 Clone

Here’s an example of the clone method in action, it’s like Deep Copy in C++.

The heap data does get copied, and s2 will have the memory in heap that holds the contents of the string.

    let s1 = String::from("hello");
    let s2 = s1.clone();

    println!("s1 = {}, s2 = {}", s1, s2);

2.4 Stack-Only Data: Copy

	let x = 5;    
	let y = x;    
	println!("x = {}, y = {}", x, y); 

After let y=x, x is still valid and wasn’t moved into y.

The reason is that types such as integers that have a known size at compile time are stored entirely on the stack,

2.4 Ownership and function

fn main() {
    let s1 = gives_ownership();         // gives_ownership moves its return
                                        // value into s1

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

    let s3 = takes_and_gives_back(s2);  // s2 is moved into
                                        // takes_and_gives_back, which also
                                        // moves its return value into s3
} // Here, s3 goes out of scope and is dropped. s2 goes out of scope but was
  // moved, so nothing happens. s1 goes out of scope and is dropped.

fn gives_ownership() -> String {             // gives_ownership will move its
                                             // return value into the function
                                             // that calls it

    let some_string = String::from("hello"); // some_string comes into scope

    some_string                              // some_string is returned and
                                             // moves out to the calling
                                             // function
}

// takes_and_gives_back will take a String and return one
fn takes_and_gives_back(a_string: String) -> String { // a_string comes into
                                                      // scope

    a_string  // a_string is returned and moves out to the calling function
}

​ When a variable that includes data on the heap goes out of scope, the value will be cleaned up by drop unless the data has been moved to be owned by another variable.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值