Rust- 智能指针

Smart pointers

A smart pointer is a data structure that not only acts like a pointer but provides additional functionality. This “smartness” comes from the fact that smart pointers encapsulate additional logical or semantic rules, which are automatically applied to simplify memory or resource management tasks.

While different programming languages implement smart pointers in various ways, they all share a common goal: to manage the life cycle of the resources they point to.

Here are some key features of smart pointers:

  1. Ownership: Smart pointers keep track of the memory they point to, and can automatically reclaim (or “free”) that memory when it’s no longer needed. This can greatly simplify memory management, as the programmer doesn’t need to manually release memory, reducing the risk of memory leaks.

  2. Reference Counting: Some smart pointers, like the Rc<T> in Rust or shared_ptr in C++, keep a count of the number of references (i.e., pointers) to an object. This object is only deallocated when there are no more references to it.

  3. Dereferencing: Like regular pointers, smart pointers implement the dereferencing operation, allowing access to the data they point to.

  4. Polymorphism: In object-oriented languages, smart pointers can be used to enable polymorphism when pointing to base and derived class objects.

  5. Thread Safety: Some smart pointers, like Arc<T> in Rust or std::atomic_shared_ptr in C++20, are thread-safe, meaning they can be safely used in concurrent programming.

In Rust, smart pointers are essential components due to the language’s focus on safety and zero-cost abstractions. Box<T>, Rc<T>, and RefCell<T> are some of the commonly used smart pointers in Rust.

  1. Box<T>: This is the simplest kind of smart pointer. It allows you to put data on the heap rather than the stack. Box<T> is often used in Rust in two scenarios: when you have a type of known size but need to store a value of a type that might have an unknown size at compile time, or when you have a large data item and want to transfer ownership to a function without copying the data to prevent stack overflow.

  2. Rc<T>: “Rc” stands for reference counting. The Rc<T> smart pointer allows your program to have multiple read-only references to the same data item on the heap. It keeps track of the number of references to the data on the heap, and once the reference count goes to zero, meaning there are no more references to the data, it cleans up the data.

  3. RefCell<T>: Rust performs borrow checking at compile time which ensures that references to data obey the rules of either one write or multiple reads. However, sometimes you may need to do such checking at runtime. RefCell<T> provides this functionality. This means that while you can borrow mutable references at runtime, your program will panic if you violate the rules of one write or multiple reads.

These are some of the basic smart pointers in Rust. There are several other smart pointers, like Arc<T>, Mutex<T>, etc., which are all designed to deal with ownership and concurrency issues in Rust.

It’s worth noting that while these types are called “smart pointers”, they’re not limited to mimicking pointer-like behavior. They’re actually more general constructs often used to add structure to a value, such as tracking reference counts, thread-safe access, and more.

Box< T >

Box<T> is a very important smart pointer in Rust. It allows you to store data on the heap rather than the stack. This is mainly used in Rust for the following two scenarios:

  1. Moving data to the heap: By default, Rust stores data on the stack. However, stack space is limited and the data on the stack is immediately removed when it goes out of scope. If you need to store a large amount of data in memory or need to pass data between scopes without losing ownership, you can use Box<T> to move the data to the heap.

  2. Storing dynamically sized types: In Rust, the size of all types must be known at compile time. However, there might be cases where you want to store a type whose size is not known at compile time. Box<T> can be used in this case, as the size of Box<T> itself is known regardless of the size of the data on the heap it points to.

When using Box<T>, Rust will automatically clean up the data on the heap when the Box<T> goes out of scope, meaning you do not need to manually manage memory.

Here is a simple example of Box<T>:

fn main() {let b = Box::new(5); // b is a pointer to a box in the heapprintln!("b = {}", b);
}

In this example, the integer 5 is stored on the heap, rather than on the stack. The variable b is a pointer to the value stored on the heap. When b goes out of scope, Rust’s automatic memory management system cleans up the data on the heap, so you do not need to manually free the memory.

use std::ops::Deref;
fn main() {/*如果一个结构体实现了deref和drop的Trait,那他们就不是普通结构体了。Rust提供了堆上存储数据的能力并把这个能力封装到了Box中。把栈上的数据搬到堆上的能力,就叫做装箱。Box可以把数据存储到堆上,而不是栈上,box 装箱,栈还是包含指向堆上数据的指针。*/let a = 6;let b = Box::new(a);println!("b = {}", b); // b = 6let price1 = 158;let price2 = Box::new(price1);println!("{}", 158 == price1);  // trueprintln!("{}", 158 == *price2); // truelet x = 666;let y = CustomBox::new(x);println!("666 == x is {}", 666 == x);   // 666 == x is trueprintln!("666 == *y is {}", 666 == *y); // 666 == *y is trueprintln!("x == *y is {}", x == *y);     // x == *y is true
}struct CustomBox<T> {value: T
}impl <T> CustomBox<T> {fn new(v: T) -> CustomBox<T> {CustomBox { value: v }}
}impl <T> Deref for CustomBox<T> {type Target = T;fn deref(&self) -> &T {&self.value}
}impl <T> Drop for CustomBox<T> {fn drop(&mut self) {println!("drop CustomBox 对象!")}
}

Rc < T >

Rc<T> stands for ‘Reference Counting’. It’s a smart pointer that allows you to have multiple owners of the same data. When each owner goes out of scope, the reference count is decreased. When the count reaches zero, the data is cleaned up.

Here’s a simple example:

use std::rc::Rc;fn main() {let data = Rc::new("Hello, World!"); // data now has a reference count of 1{let _clone = Rc::clone(&data); // data now has a reference count of 2println!("Inside the scope, data is: {}", *_clone);} // _clone goes out of scope and data's reference count decreases to 1println!("Outside the scope, data is: {}", *data);
} // data goes out of scope and its reference count decreases to 0, the data is cleaned up

RefCell < T >

RefCell<T> provides ‘interior mutability’, a design pattern in Rust that allows you to mutate data even when there are immutable references to that data. It enforces the borrowing rules at runtime instead of compile time.

Here’s a simple example:

use std::cell::RefCell;fn main() {let data = RefCell::new(5);{let mut mutable_reference = data.borrow_mut();*mutable_reference += 1;println!("Inside the scope, data is: {}", *mutable_reference);} // mutable_reference goes out of scope, the lock is releasedprintln!("Outside the scope, data is: {}", *data.borrow());
}

In this example, you can see that we borrowed a mutable reference to the data inside RefCell using borrow_mut, modified it, and then the lock was automatically released when the mutable reference went out of scope. This demonstrates the dynamic checking that RefCell<T> provides.

Note1: The borrow_mut() function is a method of RefCell<T> in Rust. This function is used to gain mutable access to the underlying value that the RefCell wraps around.

The RefCell struct in Rust enforces the borrow rules at runtime, allowing you to have multiple immutable references or one mutable reference at any point in time. If you attempt to violate these rules, your program will panic at runtime.

When you call borrow_mut() on a RefCell, you get a RefMut smart pointer that allows mutable access to the underlying data. Once this RefMut is dropped (which happens automatically when it goes out of scope), others can then borrow the RefCell again.

Here’s an example:

use std::cell::RefCell;fn main() {let data = RefCell::new(5);{let mut mutable_reference = data.borrow_mut();*mutable_reference += 1;println!("Inside the scope, data is: {}", *mutable_reference);} // mutable_reference goes out of scope, the lock is releasedprintln!("Outside the scope, data is: {}", *data.borrow());
}

In this example, mutable_reference is a mutable reference to the integer wrapped in data, which is a RefCell. We increment the integer by 1, and after mutable_reference goes out of scope, we can borrow data again.

Note2: The borrow() function is a method of RefCell<T> in Rust. This function is used to gain immutable access to the underlying value that the RefCell wraps around.

Similar to borrow_mut(), it provides a way to access the data inside the RefCell. The difference is that borrow() gives you an immutable reference (Ref<T>), while borrow_mut() gives you a mutable reference (RefMut<T>).

The RefCell checks at runtime to make sure the borrowing rules are not violated, which are:

  1. You may have either one mutable reference or any number of immutable references at the same time, but not both.
  2. References must always be valid.

If you try to call borrow() when the value is already mutably borrowed (through borrow_mut()), or try to call borrow_mut() when the value is immutably borrowed (through borrow()), your program will panic at runtime.

Here’s how you might use borrow():

use std::cell::RefCell;fn main() {let data = RefCell::new(5);{let mutable_reference = data.borrow_mut();*mutable_reference += 1;} // mutable_reference goes out of scope, the lock is releasedlet immutable_reference = data.borrow();println!("Data is: {}", *immutable_reference);
} 

In this example, we first use borrow_mut() to mutate the value inside data, then we use borrow() to get an immutable reference to the data.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/15019.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

MySQL二进制日志(binlog)配置、二进制日志binlog查看、mysqlbinlog查看二进制日志、二进制日志binlog清理等详解

提示&#xff1a;MySQL 中的日志比较重要的有 binlog&#xff08;归档日志&#xff09;、redo log&#xff08;重做日志&#xff09;以及 undo log&#xff0c;那么跟我们本文相关的主要是 binlog&#xff0c;另外两个日志松哥将来有空了再和大家详细介绍。 文章目录 1、二进制…

C++部署学习

gcc -E src/main.c -o src/main.i gcc -S src/main.c -o src/main.s gcc -C src/main.c -o src/main.o gcc src/main.c -o exec ./exec

累加和最大的组合

1、题目 给定一个数组,选择数字组成组合,请问哪个组合的累加和最大。 要求:相邻的数不能同时选。 例子: 输入:[3, 7, 9] 输出:12。选择 3 和 92、思路 定义dp[i],表示在 arr 的 0 ~ i i i 范围上按照选择数组成组合,累加和最大的结果,即所有可能性的最优。 dp[…

RabbitMQ 教程 | 第3章 客户端开发向导

&#x1f468;&#x1f3fb;‍&#x1f4bb; 热爱摄影的程序员 &#x1f468;&#x1f3fb;‍&#x1f3a8; 喜欢编码的设计师 &#x1f9d5;&#x1f3fb; 擅长设计的剪辑师 &#x1f9d1;&#x1f3fb;‍&#x1f3eb; 一位高冷无情的编码爱好者 大家好&#xff0c;我是 DevO…

20230727-随笔

目录 List删除满足条件的元素&#xff0c;并且避免索引错误或并发修改异常常用方法使用迭代器删除元素通过逆向循环删除元素Java8 的 removeIf()方法 获取不到日志内容问题排查尝试解决最终解决 List删除满足条件的元素&#xff0c;并且避免索引错误或并发修改异常常用方法 使…

排序算法汇总

每日一句&#xff1a;你的日积月累终会成为别人的望尘莫及 目录 常数时间的操作 选择排列 冒泡排列 【异或运算】 面试题&#xff1a; 1&#xff09;在一个整形数组中&#xff0c;已知只有一种数出现了奇数次&#xff0c;其他的所有数都出现了偶数次&#xff0c;怎么找到…

51单片机IO口控制

51单片机IO口控制 1.点亮LED灯 原理&#xff1a;根据电路图&#xff0c;指向IO口的引脚&#xff1b;拉低电平&#xff0c;灯亮、 如图&#xff1a; [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Zfco4IjK-1690308697530)(C:/Users/xie19/Pictur…

C++基础篇(一)常用关键字及示例

一、C常见关键词 1、auto auto: 自动类型推断。它可以让编译器根据变量的初始值自动推断出变量的类型。例如&#xff1a; auto x 42; // x 的类型为 int auto y 3.14; // y 的类型为 double2、decltype decltype: 类型推断。它可以根据表达式的类型推断出一个类型。例如&…

面试之CurrentHashMap的底层原理

首先回答HashMap的底层原理? HashMap是数组链表组成。数字组是HashMap的主体&#xff0c;链表则是主要为了解决哈希冲突而存在的。要将key 存储到&#xff08;put&#xff09;HashMap中&#xff0c;key类型实现必须计算hashcode方法&#xff0c;默认这个方法是对象的地址。接…

input元素中的form属性有什么用?

在HTML中&#xff0c;input元素的form属性用于指定该输入字段所属的表单&#xff08;form元素&#xff09;。通过将input元素的form属性设置为相应的表单的id值&#xff0c;可以将输入字段与表单进行关联。 这个属性对于两个主要目的非常有用&#xff1a; 表单关联&#xff1…

【应用层】Http协议总结

文章目录 一、续->Http协议的学习 1.http请求中的get方法和post方法 2.http的状态码 3.http的报头 4.长链接 5.cookie&#xff08;会话保持&#xff09;总结 继续上一篇的内容&#xff1a; 上一篇的最后我们讲到了web根目录&#xff0c;知道…

epoll服务器创建

驱动 #include <linux/init.h> #include <linux/module.h> #include <linux/fs.h> #include <linux/io.h> #include <linux/device.h> #include <linux/uaccess.h> #include <linux/poll.h> unsigned int major; char kbuf[128]{0}…

使用Docker部署EMQX

原文链接&#xff1a;http://www.ibearzmblog.com/#/technology/info?id9dd5bf4159d07f6a4e69a6b379ce4244 前言 在物联网中&#xff0c;大多通信协议使用的都是MQTT&#xff0c;而EMQX是基于 Erlang/OTP 平台开发的 MQTT 消息服务器&#xff0c;它的优点很多&#xff0c;我…

《吐血整理》进阶系列教程-拿捏Fiddler抓包教程(12)-Fiddler设置IOS手机抓包,你知多少???

1.简介 Fiddler不但能截获各种浏览器发出的 HTTP 请求&#xff0c;也可以截获各种智能手机发出的HTTP/ HTTPS 请求。 Fiddler 能捕获Android 和 Windows Phone 等设备发出的 HTTP/HTTPS 请求。同理也可以截获iOS设备发出的请求&#xff0c;比如 iPhone、iPad 和 MacBook 等苹…

【BMC】OpenBMC使用基础(WSL2版本)

代码准备 OpenBMC是一个开源的项目&#xff0c;用于开发BMC固件。官网是https://www.openbmc.org/&#xff0c;不过里面似乎没有什么内容&#xff0c;所以还需要依赖其它的网站&#xff0c;https://github.com/openbmc&#xff0c;在这里可以下载到需要的代码和文档。其主体部…

C#,数值计算——对数正态分布(logarithmic normal distribution)的计算方法与源程序

对数正态分布&#xff08;logarithmic normal distribution&#xff09;是指一个随机变量的对数服从正态分布&#xff0c;则该随机变量服从对数正态分布。对数正态分布从短期来看&#xff0c;与正态分布非常接近。但长期来看&#xff0c;对数正态分布向上分布的数值更多一些。 …

Tailwind CSS:基础使用/vue3+ts+Tailwind

一、理解Tailwind 安装 - TailwindCSS中文文档 | TailwindCSS中文网 Installation - Tailwind CSS 1.1、词义 我们简单理解就是搭上CSS的顺风车&#xff0c;事半功倍。 1.2、Tailwind CSS有以下优势 1. 快速开发&#xff1a;Tailwind CSS 提供了一些现成的 class / 可复用…

ARM裸机-4

1、什么是交叉编译 1.1、两种开发模式 非嵌入式开发&#xff0c;A&#xff08;类&#xff09;机编写&#xff08;源代码&#xff09;、编译得到可执行程序&#xff0c;发布给A&#xff08;类&#xff09;机运行。 嵌入式开发&#xff0c;A&#xff08;类&#xff09;机编写&am…

webpack : 无法加载文件 C:\Program Files\nodejs\webpack.ps1

webpack : 无法加载文件 C:\Program Files\nodejs\webpack.ps1 1.问题2. 解决办法&#xff1a; 1.问题 使用webpack打包是报错如下&#xff1a; webpack : 无法加载文件 C:\Program Files\nodejs\webpack.ps1&#xff0c;因为在此系统上禁止运行脚本。有关详细信息&#xff0c…

Spring源码(三)Spring Bean生命周期

Bean的生命周期就是指&#xff1a;在Spring中&#xff0c;一个Bean是如何生成的&#xff0c;如何销毁的 Bean生命周期流程图 1、生成BeanDefinition Spring启动的时候会进行扫描&#xff0c;会先调用org.springframework.context.annotation.ClassPathScanningCandidateCompo…