Rust 模式匹配中的& 和 ref

一、Rust & 和 ref

1.Rust的ref有什么用

根据Rust官方文档https://doc.rust-lang.org/std/keyword.ref.html

Rust ref 主要用在模式匹配match的所有权问题中。

ref在 Rust中,也是声明一个指针类型变量,进一步说明ref和&在其它方面区别,我们下一篇再说。

Rust的模式匹配 ,可以发生在函数参数match

  1. 函数参数
rust 复制代码
fn foo(x: String) {//String::from("test")的所有权已经移交给了x
  println!("{}",x);
  // 代码
}

fn main() {
    let mut s=String::from("test");
    foo(s);
    println!("{}",s);//s的所有权已经丢失,所以不能使用了,此处出错
}

特别注意一下,函数传参 也会交出所有权

2.match

rust 复制代码
fn main() {
    let x = String::from("test");

    match x {
        y => println!("{}", y),// String::from("test")的所有权移动给了y
        _ => println!("Default case, x = {:?}", x),
    }

    println!("at the end: x = {:?}", x);//x的所有权已经丢失,不能再使用。
}

特别注意一下,match的模式匹配 是会交出所有权的。

除了使用&引用借用来避免交出所有权的问题,在模式匹配中,我们可以使用**&或ref**来避免交出所有权,match中的ref不再遵循借用规则。那么前面的代码我们可以修改为。

  1. 函数参数使用**&避免**交出所有权
rust 复制代码
fn foo(x: &mut String) {//
  println!("{}",x);
  // 代码
}

fn main() {
    let mut s=String::from("test");
    foo(&mut s);//(可变/不可变)引用,不拥有所有权
    println!("{}",s);
}

代码运行通过

bash 复制代码
    Finished release [optimized] target(s) in 0.20s
     Running `target/release/world_hello`
test
test

2、match 使用ref避免交出所有权

rust 复制代码
fn main() {
    let x = String::from("test");

    match x {
        ref y => println!("{}", y),// 其实在match中,不是定义一个变量,而是声明一个变量,ref是进一步声明,y是一个引用,但是不能够&y,因为我们不能够在match中声明y的具体类型。
        _ => println!("Default case, x = {:?}", x),
    }

    println!("at the end: x = {:?}", x);
}

代码运行通过

bash 复制代码
    Finished release [optimized] target(s) in 0.20s
     Running `target/release/world_hello`
test
test

其实这里你可能会问,为什么 在match中,不使用&借用避免所有权移动 的问题,这是因为match本身机制导致的。

在match中,我们没有机会声明变量类型,不能用&修饰匹配的变量

当然,你非要在**match使用&**来避免移交所有权的问题,我们可以这样做

rust 复制代码
fn main() {
  let x = String::from("test");

  match &x {//将引用定义在这里
      y => println!("{}", y),//这里不能够写成&y => println!("{}", y),
      _ => println!("Default case, x = {:?}", x),
  }

  println!("at the end: x = {:?}", x);
}

代码运行也是成功的

bash 复制代码
    Finished release [optimized] target(s) in 0.22s
     Running `target/release/world_hello`
test
at the end: x = "test"

我们再来看一个骚的

rust 复制代码
// 修复错误,尽量少地修改代码
// 不要移除任何代码行
fn main() {
    let mut v = String::from("hello,");
    let r = &mut v;

    match r {
       &mut value => value.push_str(" world!") 
    }
}

前面我们说了 ,我们不能声明匹配变量,也就是value,那么**&mut 是肯定不能用**的,其实第一版修改,我们可以改成

rust 复制代码
// 修复错误,尽量少地修改代码
// 不要移除任何代码行
fn main() {
  let mut v = String::from("hello,");
  let r = &mut v;

  match r {
     value => value.push_str(" world!") //这样其实value就是可变引用,但是&mut v本身的所有权被value拿走了
  }
}

根据代码注释,如果我们稍加修改就会发现问题

rust 复制代码
// 修复错误,尽量少地修改代码
// 不要移除任何代码行
fn main() {
  let mut v = String::from("hello,");
  let r = &mut v;

  match r {
     value => value.push_str(" world!") 
  }
  println!("{}",r);
}

运行代码

bash 复制代码
error[E0382]: borrow of moved value: `r`
  --> src/main.rs:11:17
   |
6  |   let r = &mut v;
   |       - move occurs because `r` has type `&mut String`, which does not implement the `Copy` trait
...
9  |      value => value.push_str(" world!") 
   |      ----- value moved here
10 |   }
11 |   println!("{}",r);
   |                 ^ value borrowed here after move

这里说了,&mut v的所有权被value拿走了

那我不想被value拿走,我们应该怎么做,那么这时候ref的作用就来了,我们可以这么修改

rust 复制代码
// 修复错误,尽量少地修改代码
// 不要移除任何代码行
fn main() {
  let mut v = String::from("hello,");
  let r = &mut v;

  match *r {//注意这里是*r,因为r本身就是&str,如果传下去r,ref r 就变成了双重引用,不符合题意
     ref mut value => value.push_str(" world!") //此时value就是&str类型,但是这不是引用类型
  }
  println!("{}",r);
}

运行代码

bash 复制代码
    Finished release [optimized] target(s) in 0.34s
     Running `target/release/world_hello`
hello, world!

前面我们说了,ref不遵循借用的那一套规则,所以上面的代码是可以运行成功的。

按照正常思路的话(假设),按道理应该是两个可变引用,是违背借用原则的,会不会是match{}框住了value的作用域导致,其实只有一个可变引用?

那我们在看一段代码

rust 复制代码
// 修复错误,尽量少地修改代码
// 不要移除任何代码行
fn main() {
  let mut v = String::from("hello,");
  let r = &mut v;

  match *r {
     ref mut value => {
      value.push_str(" world!") ;
      println!("{}",r);
    }
  }
  println!("{}",r);
}

运行一下

bash 复制代码
    Finished release [optimized] target(s) in 0.21s
     Running `target/release/world_hello`
hello, world!
hello, world!

哈哈,我们已经验证完了

ref不遵循借用的那一套规则,在match的模式匹配中,ref也可以是不拿所有权的一种引用方法。

相关推荐
冬奇Lab7 分钟前
每日一个开源项目(第154篇):Warp - 从‘好看的终端‘到 Agentic 开发环境
人工智能·rust·llm
犀利豆1 小时前
AI in Harness(三)
人工智能·后端
多加点辣也没关系2 小时前
JavaScript|第4章:类型转换
开发语言·javascript
聪慧的水蜜桃2 小时前
【YFIOs】用C#开发硬件之设备上云
开发语言·c#
yqcoder2 小时前
httpOnly 是什么,又有什么用?
开发语言·前端·javascript
IT_陈寒2 小时前
Java的Stream.parallel()把我CPU跑爆了,这种优化要谨慎
前端·人工智能·后端
子兮曰3 小时前
Bun 重写为 Rust:11天、64个Claude、16.5万美元,一次改变行业认知的激进实验
前端·后端·bun
子兮曰3 小时前
Node.js v26.5.0 深度解读:import Text、流式 ReadableStreamTee、以及隐藏的安全加固浪潮
前端·后端·node.js
子兮曰3 小时前
TypeScript 7.0 RC 深度解读:Go 重写完成,10 倍提速,编译器的「奇点时刻」
前端·后端·typescript
kosan3 小时前
.NET/C# 实战:我如何把登录验证码从“裸奔”改到“铜墙铁壁”
后端