欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 国际 > 从零开始学Rust:所有权(Ownership)机制精要

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

2025/4/3 3:35:26 来源:https://blog.csdn.net/Vitalia/article/details/146783340  浏览:    关键词:从零开始学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 Stringprintln!("{}", 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.

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词