IB-Rust-引用

引用的分类
- 不可变引用(Immutable Reference)
- 可以读取数据,但不能修改
- 一个变量可以有多个不可变引用,但不能与可变引用共存
- 可变引用(Mutable Reference)
- 可以读取和修改数据
- 一个变量在某一时刻只能有一个可变引用,且不能与不可变引用共存
借用的规则
- 同一时间内,一个变量只能有一个可变引用或多个不可变引用
- 引用必须总是有效(被引用的数据在其引用的生命周期内必须始终存在,且不能被销毁)
move & borrow & slice
move
当堆数据被赋值给另一个变量或作为参数传递时,其所有权会 转移(move)到新变量或函数中,原变量将失效
borrow
借用是 Rust 中通过引用 临时访问数据 的逻辑概念
slice
切片(slices)分为:
字符串切片
1 | let s = String::from("hello world"); |
数组切片
1 | let arr = [1, 2, 3, 4, 5]; |
课后习题
通过编译器错误提示,修复并运行代码
1 |
|
如果函数返回引用,且该引用依赖于输入参数的引用,必须显式标注生命周期参数
#[test]
fn test_lifetime() {
let large = longest("a", "ab");
println!("large one is {large}");
// modified
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() {
x
} else {
y
}
}
}
- Title: IB-Rust-引用
- Author: Gabrielle
- Created at : 2025-05-22 16:26:31
- Updated at : 2025-05-22 17:22:21
- Link: https://zoella-w.github.io/2025/05/22/65-IB-Rust-引用/
- License: This work is licensed under CC BY-NC-SA 4.0.