rust学习-所有权

rust学习-所有权

Rust 的所有权系统是其最大的特色之一,也是 Rust确保内存安全和避免空指针、数据竞争等错误的关键机制,所有权系统的核心概念包括所有权、引用和生命周期

所有权

rust中每个值都有一个与其相关联的变量,称为其所有者,所有权遵循以下规则:

  • 每个值都有一个所有者
  • 值在任何时刻都只能有一个所有者
  • 当所有者离开作用域时,该值将被丢弃
rust 复制代码
{
    let s = String::from("hello");  // s 进入作用域

    // 使用 s

}  // s 离开作用域,被丢弃

在这个示例中,s 是 String::from("hello") 的所有者,当 s 离开作用域时,String 被自动丢弃,释放其占用的内存

移动所有权

当将一个值赋给另一个变量时,所有权会被转移,原始变量不再有效

rust 复制代码
fn main() {
    let s1 = String::from("hello");
    let s2 = s1;  // s1 的所有权被转移给 s2

    // println!("{}", s1);  // 这里会报错,因为 s1 已经不再有效
    println!("{}", s2);  // 正确,s2 拥有字符串
}

在这个示例中,s1 的所有权被转移给 s2,因此 s1 在转移后不能再使用

复制和克隆

对于简单数据类型,如整数和布尔值,Rust 会自动复制它们,因此不会发生所有权转移

rust 复制代码
let x = 5;
let y = x;  // x 被复制,y 也等于 5

对于复杂类型,如 String,需要显式地克隆数据

rust 复制代码
let s1 = String::from("hello");
let s2 = s1.clone();  // 克隆 s1 的数据

引用

引用可使用变量的值而不转移所有权,引用必须是 immutable(不可变)的,除非显式指定为 mutable(可变)

rust 复制代码
let s1 = String::from("hello");
let len = calculate_length(&s1);  // 传递引用
println!("The length of '{}' is {}.", s1, len);

fn calculate_length(s: &String) -> usize {
    s.len()
}

在这个示例中,&s1 是一个对 s1 的引用,calculate_length 函数接受一个 &String 参数

可变引用

要修改通过引用传递的值,需要使用可变引用

rust 复制代码
let mut s = String::from("hello");
change(&mut s);

fn change(some_string: &mut String) {
    some_string.push_str(", world");
}

在这个示例中,&mut s 是一个可变引用,允许在 change 函数中修改 s 的值

生命周期

生命周期是 Rust 编译器用于确保引用总是有效的机制,编译器会检查引用的生命周期,确保它们不会超过所引用数据的生命周期

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

在这个示例中,'a 是一个生命周期参数,表示返回的引用至少和传入的引用一样长

所有权和函数

函数可以接受所有权或引用作为参数

接受所有权

rust 复制代码
fn takes_ownership(some_string: String) {
    // some_string 现在是这个函数的局部变量
    println!("{}", some_string);
}

fn main() {
    let s = String::from("hello");
    takes_ownership(s);
    // s 现在不再有效
}

接受引用

rust 复制代码
fn calculates_length(s: &String) -> usize {
    s.len()
}

fn main() {
    let s = String::from("hello");
    let length = calculates_length(&s);
    println!("The length of '{}' is {}.", s, length);
}

返回值和作用域

函数可以返回值的所有权

rust 复制代码
fn gives_ownership() -> String {
    let some_string = String::from("hello");
    some_string  // 返回 some_string 的所有权
}

fn main() {
    let s = gives_ownership();
    println!("{}", s);
}

注意事项

  • 避免悬挂引用:不要返回对局部变量的引用,因为局部变量在函数结束时会被丢弃
rust 复制代码
fn bad() -> &String {
    let s = String::from("hello");
    &s  // 错误,s 在函数结束时被丢弃
}
  • 不可变引用和可变引用的规则
    • 在同一作用域内,只能有多个不可变引用或一个可变引用
    • 不能在有可变引用的同时有不可变引用
rust 复制代码
let mut s = String::from("hello");

let r1 = &s;  // 不可变引用
let r2 = &s;  // 另一个不可变引用
// let r3 = &mut s;  // 错误,已有不可变引用

println!("{} and {}", r1, r2);
drop(r1); drop(r2);

let r3 = &mut s;  // 可变引用
println!("{}", r3);
  • 生命周期注解:对于复杂的情况,可能需要显式指定生命周期
rust 复制代码
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
    if x.len() > y.len() {
        x
    } else {
        y
    }
}
  • Deref 自动引用转换:Rust 的自动解引用(deref coercions)可以隐式地将引用转换为其他类型的引用
rust 复制代码
struct Person {
    name: String,
    age: u8,
}

fn print_name(p: &Person) {
    println!("Name: {}", p.name);
}

fn main() {
    let person = Person { name: String::from("Alice"), age: 30 };
    print_name(&person);
}
相关推荐
liulilittle7 小时前
Unix/Linux 平台通过 IP 地址获取接口名的 C++ 实现
linux·开发语言·c++·tcp/ip·unix·编程语言
起风了___7 小时前
20 分钟搞定:Jenkins + Docker 一键流水线,自动构建镜像并部署到远程服务器
后端
用户4099322502127 小时前
如何在 FastAPI 中巧妙覆盖依赖注入并拦截第三方服务调用?
后端·ai编程·trae
Source.Liu7 小时前
【Python基础】 18 Rust 与 Python print 函数完整对比笔记
笔记·python·rust
Nerd Nirvana7 小时前
C++编程——异步处理、事件驱动编程和策略模式
开发语言·c++·策略模式·嵌入式开发·事件驱动·异步处理
泉城老铁8 小时前
Spring Boot中实现多线程分片下载
java·spring boot·后端
泉城老铁8 小时前
Spring Boot中实现多文件打包下载
spring boot·后端·架构
泉城老铁8 小时前
Spring Boot中实现大文件分片下载和断点续传功能
java·spring boot·后端
2501_920047038 小时前
bash自带的切片操作
开发语言·python·bash
码事漫谈8 小时前
C++中虚函数与构造/析构函数的深度解析
后端