当前位置:网站首页>Rust简短笔记:4种不同的引用变量解答
Rust简短笔记:4种不同的引用变量解答
2022-07-21 05:08:00 【biakia0610】
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
经过上面三种情况的讨论,我们可以知道这种写法的意思其实是:表示该引用可变,并且引用指向的内容可修改。
至于例子就留给读者去尝试吧~
边栏推荐
- web安全--文件包含(本地包含,远程包含)
- PHP implements bidirectional linked list
- Thinkphp5.1 send email with phpmailer
- Buuctf n1book [Chapter 2 advanced web] file upload
- PHP環境搭建(推薦寶塔面板)
- COMSOL heat conduction method to solve maze problem (path planning)
- Recurrence of the web question of the second online blade cup
- Téléchargement de fichiers buctf n1book [chapitre 2 Web Advanced]
- Construction de l'environnement PHP (panneau de pagode recommandé)
- Thinkphp6 uses easywechat5 Development of official account of X (I)
猜你喜欢
[intranet penetration] intranet penetration of vulnstack II
【逆向分析】基础入门-查找程序main函数修改程序
Thinkphp6 learning experience
BUUCTF [GXYCTF2019] 禁止套娃
【内网渗透】openssl反弹流量加密shell
Solve the problem of vendor JS file is too large (official processing scheme)
Fundamentals of computational heat transfer
解决uniapp编译后vendor.js文件过大(官方处理方案)
[permission promotion] MSSQL authorization raising method
Attack and defense World Web Zone difficulty level: 2 (upload1, web2, web_php_include, supersqli, warmup)
随机推荐
[file upload bypass] - Secondary rendering
Téléchargement de fichiers buctf n1book [chapitre 2 Web Advanced]
[intranet penetration] OpenSSL rebound traffic encrypted shell
TP5对接免签FM支付接口
【逆向分析】恶意代码静态分析
Typewriter typing, backspace effect
DVWA [SQL injection (blind)] learning record
24. [judge whether it is an integer with bytes]
【极客大挑战 2019】Easy,Love,Baby-SQL
BUUCTF n1book [第二章 web進階]文件上傳
Magic method of PHP
Moher college webshell file upload analysis (questions 3-5)
PhpMyAdmin stage file contains vulnerability analysis
PHP 大文件分块上传 底层实现
Vulnhub-dc-4 target penetration record
Koa2 fast build server
Help the great God
Invalid mouse disabled style (cursor: not allowed) conflicts with mouse disabled events (pointer events: none)
[geek challenge 2019] easy, love, baby SQL
web安全--文件上传中间件解析漏洞