当前位置: 首页 > news >正文

从零开始学Rust:所有权(Ownership)机制精要

文章目录

  • 第四章:Ownership 所有权
    • 核心概念
    • 关键机制
    • 引用与借用(Reference & Borrowing)
      • 悬垂引用问题
        • 错误示例分析
        • 解决方案
        • 引用安全规则
    • 切片(Slice)
    • 内存安全保证

第四章:Ownership 所有权

Ownership is Rust’s most unique feature, and it enables Rust to make memory safety guarantees without needing a garbage collector.

核心概念

所有权三原则:

  • 每个值有且只有一个所有者
  • 所有权可转移(move)
  • 所有者离开作用域时值自动释放(drop)
  • 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.

内存管理:

  • 栈(Stack):固定大小数据,高效自动管理
  • 堆(Heap):动态大小数据,需显式分配

Example:

fn main() {
    let mut s = String::from("hello");
    s.push_str(", world!"); // push_str() appends a literal to a String
    println!("{}", s); // This will print `hello, world!`
}

Note: In C++, this pattern of deallocating resources at the end of an item’s lifetime is sometimes called Resource Acquisition Is Initialization (RAII). The drop function in Rust will be familiar to you if you’ve used RAII patterns.

关键机制

  • 移动语义(Move):

    • 赋值操作默认转移所有权(非浅拷贝)
    • 原变量随即失效,防止悬垂指针
    let s1 = String::from("hello");
    let s2 = s1;  // s1所有权转移至s2,s1失效
    

    If you’ve heard the terms shallow copy and deep copy while working with other languages, the concept of copying the pointer, length, and capacity without copying the data probably sounds like making a shallow copy. But because Rust also invalidates the first variable, instead of being called a shallow copy, it’s known as a move. In this example, we would say that s1 was moved into s2.
    在这里插入图片描述
    Rust will never automatically create “deep” copies of your data. Therefore, any automatic copying can be assumed to be inexpensive in terms of runtime performance.

  • 克隆(Clone):

    • 显式深度拷贝堆数据
    let s1 = String::from("hello");
    let s2 = s1.clone();  // 完整拷贝数据
    

    When you see a call to clone, you know that some arbitrary code is being executed and that code may be expensive. It’s a visual indicator that something different is going on.
    在这里插入图片描述

  • Copy trait:

    • 标量类型(整数、布尔值等)自动实现
    • 赋值时执行位拷贝而非所有权转移
    • 与Drop trait互斥

引用与借用(Reference & Borrowing)

  • 不可变引用:

    • 允许多个同时存在
    • 禁止修改数据
    fn calculate_length(s: &String) -> usize {
        s.len()
    }
    
  • 可变引用:

    • 独占性:同一作用域只能存在一个
    • 数据竞争预防
    fn change(s: &mut String) {
        s.push_str(", world");
    }
    

悬垂引用问题

悬垂引用(Dangling Reference)是指指针指向的内存已经被释放但指针仍然存在的情况,这是内存安全的重大威胁。

错误示例分析
fn dangle() -> &String {  // 尝试返回字符串引用
    let s = String::from("hello");  // 创建局部变量
    &s  // 返回局部变量的引用
}  // s离开作用域被释放,返回的引用变成悬垂指针

编译器会阻止这段代码编译,错误提示:

error[E0106]: missing lifetime specifier
解决方案
  1. 转移所有权
fn no_dangle() -> String {  // 直接返回String(转移所有权)
    let s = String::from("hello");
    s  // 所有权转移给调用者
}
  1. 使用静态生命周期
fn get_static_str() -> &'static str {
    "hello"  // 字符串字面量有'static生命周期
}
引用安全规则
  1. 可变性独占原则:
  • 任意时刻,一个数据要么:
    • 有唯一的可变引用(&mut T)
    • 或有多个不可变引用(&T)
  • 二者不可同时存在
  1. 生命周期保证:
  • 所有引用必须始终有效

  • 编译器通过生命周期检查确保:

    fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
        if x.len() > y.len() { x } else { y }
    }
    

切片(Slice)

  • 字符串切片:
    • 安全视图,不获取所有权
    let s = String::from("hello world");
    
    let hello = &s[0..5];
    let world = &s[6..11];
    
  • 通用原则:
    • 编译时保证引用有效性
    • 自动边界检查防止越界访问

在这里插入图片描述

内存安全保证

所有权系统在编译期实现:

  • 自动内存回收(RAII模式)
  • 无数据竞争
  • 无悬垂指针
  • 零运行时开销

这套机制使Rust无需垃圾回收器即可保证内存安全,同时保持与C/C++相当的性能。

The concepts of ownership, borrowing, and slices ensure memory safety in Rust programs at compile time. The Rust language gives you control over your memory usage in the same way as other systems programming languages, but having the owner of data automatically clean up that data when the owner goes out of scope means you don’t have to write and debug extra code to get this control.

相关文章:

  • Android版本更新服务通知下载实现
  • C++编程指南31 - 除非绝对必要,否则不要使用无锁编程
  • BERT与Transformer到底选哪个-上部
  • 福建省公共数据授权运营实践案例详解(运营机制及模式、运营单位、运营平台、场景案例等)
  • hadoop 集群的常用命令
  • PyTorch量化进阶教程:第六章 模型部署与生产化
  • 【套题】大沥2019年真题——第1~3题
  • Python扩展知识详解:lambda函数
  • 实现在Unity3D中仿真汽车,而且还能使用ros2控制
  • 【Yolov8部署】 VS2019+opencv+onnxruntime 环境下部署目标检测模型
  • Spring框架中的IoC(控制反转)
  • 【MachineLearning】生成对抗网络 (GAN)
  • VRRP协议
  • java详细笔记总结持续完善
  • Linux安装Idea
  • Vue3中的Icon处理方案(包括将svg转化为Icon)
  • 单北斗:构筑自主时空基准,赋能数字中国新未来
  • linux0.11内核源码修仙传第十二章——内核态到用户态
  • vue3 根据城市名称计算城市之间的距离
  • 【系统性偏见:AI照出的文明暗伤与生存悖论】
  • 石家庄 科技 公司 网站建设/世界军事新闻
  • wordpress页面限制/网站搜索引擎优化主要方法
  • 专业网站建设报价/网络推广方案七步法
  • 常用的搜索引擎的网站/seo简单优化
  • 网站怎么自己优化/seo优化关键词放多少合适
  • 建设政府网站的意义/建站之星