Rust简短笔记:4种不同的引用变量解答

Rust定义引用变量其实一共有四种写法:

1. a: &T

2. a:&mut T

3. mut a:&T

4. mut a:&mut T

下面分别阐述这四种写法

1、a: &T

这种引用变量的意思是:引用不可变,并且引用指向的内容不可修改。

举个例子:

#[derive(Debug)]
struct Account{
    count:u32
}

fn main() {
  let a:&Account = &Account{count:11};
  println!("{r:p},{r:?}",r=a);
}

最终打印:

0x5626a883f000,Account { count: 11 }

这个例子里a是一个结构体Account的引用,并且a所引用的内容无法被修改,如果修改会怎么样?看下面的例子:

#[derive(Debug)]
struct Account{
    count:u32
}

fn main() {
  let a:&Account = &Account{count:11};
  a.count = 12;
  println!("{r:p},{r:?}",r=a);
}

这时候编译会报错:

  Compiling playground v0.0.1 (/playground)
error[E0594]: cannot assign to `a.count`, which is behind a `&` reference
  --> src/main.rs:10:3    |
9  |   let a:&Account = &Account{count:11};
   |                    ------------------ help: consider changing this to be a mutable reference: `&mut Account{count:11}`
10 |   a.count = 12;
   |   ^^^^^^^^^^^^ `a` is a `&` reference, so the data it refers to cannot be written

从错误信息可以看出(`a` is a `&` reference, so the data it refers to cannot be written)a所引用的数据无法被修改。

那如果我重新生成一个Account然后让a引用会怎么样呢?看下面的例子:

#[derive(Debug)]
struct Account{
    count:u32
}

fn main() {
  let a:&Account = &Account{count:11};
  let b:&Account = &Account{count:12};
  a = b;
  println!("{r:p},{r:?}",r=a);
}

这时候编译会报错:

error[E0384]: cannot assign twice to immutable variable `a`
  --> src/main.rs:11:3    |
9  |   let a:&Account = &Account{count:11};
   |       -
   |       |
   |       first assignment to `a`
   |       help: consider making this binding mutable: `mut a`
10 |   let b:&Account = &Account{count:12};
11 |   a = b;
   |   ^^^^^ cannot assign twice to immutable variable

从错误信息可以看出,a是一个不可变量(immutable variable),无法被重新赋值。

总结:a:&T 表示该引用不可变,并且引用指向的内容不可修改。

2、a:&mut T

这种引用变量的意思是:引用不可变,引用指向的内容可修改。

举个例子:

#[derive(Debug)]
struct Account{
    count:u32
}

fn main() {
  let a:&mut Account = &mut Account{count:11};
  a.count = 12;
  println!("{r:p},{r:?}",r=a);
}

此时打印:

0x7ffe8075ba54,Account { count: 12 }

如果想要改变a的指向:

#[derive(Debug)]
struct Account{
    count:u32
}

fn main() {
  let a:&mut Account = &mut Account{count:11};
  let b:&mut Account = &mut Account{count:12};
  a = b;
  println!("{r:p},{r:?}",r=a);
}

此时就会报上面同样的错误:

error[E0384]: cannot assign twice to immutable variable `a`
  --> src/main.rs:11:3    |
9  |   let a:&mut Account = &mut Account{count:11};
   |       -
   |       |
   |       first assignment to `a`
   |       help: consider making this binding mutable: `mut a`
10 |   let b:&mut Account = &mut Account{count:12};
11 |   a = b;
   |   ^^^^^ cannot assign twice to immutable variable

说明a是无法重新设置的。

总结:a:&mut T 表示该引用不可变,引用指向的内容可修改。

3、mut a:&T

这种引用变量的意思是:引用可重新赋值,但引用指向的内容不可修改。

下面先看个正常的例子:

#[derive(Debug)]
struct Account{
    count:u32
}

fn main() {
  let mut a:&Account = &Account{count:11};
  let b:&Account = &Account{count:12};
  a = b;
  println!("{r:p},{r:?}",r=a);
}

打印结果如下:

0x5615b6e50004,Account { count: 12 }

如果我们尝试修改Account的内容:

#[derive(Debug)]
struct Account{
    count:u32
}

fn main() {
  let mut a:&Account = &Account{count:11};
  a.count = 12;
  println!("{r:p},{r:?}",r=a);
}

就会得到如下错误:

error[E0594]: cannot assign to `a.count`, which is behind a `&` reference
  --> src/main.rs:10:3    |
9  |   let mut a:&Account = &Account{count:11};
   |                        ------------------ help: consider changing this to be a mutable reference: `&mut Account{count:11}`
10 |   a.count = 12;
   |   ^^^^^^^^^^^^ `a` is a `&` reference, so the data it refers to cannot be written

说明a指向的内容不可修改。

总结:mut a:&T 表示该引用可变,但引用指向的内容不可修改。

4、mut a:&mut T

经过上面三种情况的讨论,我们可以知道这种写法的意思其实是:表示该引用可变,并且引用指向的内容可修改。

至于例子就留给读者去尝试吧~

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值