rust-by-practice/zh-CN/src/ownership/borrowing.md

171 lines
2.7 KiB
Markdown
Raw Permalink Normal View History

2022-02-25 08:57:13 -06:00
# 引用和借用
### 引用
2022-03-02 08:11:56 -06:00
1. 🌟
2022-02-25 08:57:13 -06:00
```rust,editable
fn main() {
let x = 5;
// 填写空白处
let p = __;
println!("x 的内存地址是 {:p}", p); // output: 0x16fa3ac84
}
```
2022-03-02 08:11:56 -06:00
2. 🌟
2022-02-25 08:57:13 -06:00
```rust,editable
fn main() {
let x = 5;
let y = &x;
// 只能修改以下行
assert_eq!(5, y);
}
```
2022-03-02 08:11:56 -06:00
3. 🌟
2022-02-25 08:57:13 -06:00
```rust,editable
// 修复错误
fn main() {
let mut s = String::from("hello, ");
borrow_object(s)
}
fn borrow_object(s: &String) {}
```
2022-03-02 08:11:56 -06:00
4. 🌟
2022-02-25 08:57:13 -06:00
```rust,editable
// 修复错误
fn main() {
let mut s = String::from("hello, ");
2022-03-02 06:57:13 -06:00
push_str(s)
2022-02-25 08:57:13 -06:00
}
2022-03-02 06:57:13 -06:00
fn push_str(s: &mut String) {
s.push_str("world")
}
2022-02-25 08:57:13 -06:00
```
2022-03-02 08:11:56 -06:00
5. 🌟🌟
2022-02-25 08:57:13 -06:00
```rust,editable
fn main() {
let mut s = String::from("hello, ");
// 填写空白处,让代码工作
let p = __;
p.push_str("world");
}
```
#### ref
`ref``&` 类似,可以用来获取一个值的引用,但是它们的用法有所不同。
2022-03-02 08:11:56 -06:00
6. 🌟🌟🌟
2022-02-25 08:57:13 -06:00
```rust,editable
fn main() {
let c = '中';
let r1 = &c;
// 填写空白处,但是不要修改其它行的代码
let __ r2 = c;
assert_eq!(*r1, *r2);
// 判断两个内存地址的字符串是否相等
assert_eq!(get_addr(r1),get_addr(r2));
}
// 获取传入引用的内存地址的字符串形式
fn get_addr(r: &char) -> String {
format!("{:p}", r)
}
```
### 借用规则
2022-03-02 08:11:56 -06:00
7. 🌟
2022-02-25 08:57:13 -06:00
```rust,editable
// 移除代码某个部分,让它工作
// 你不能移除整行的代码!
fn main() {
let mut s = String::from("hello");
let r1 = &mut s;
let r2 = &mut s;
println!("{}, {}", r1, r2);
}
```
#### 可变性
2022-03-06 02:34:37 -06:00
8. 🌟 错误: 从不可变对象借用可变
2022-02-25 08:57:13 -06:00
```rust,editable
fn main() {
// 通过修改下面一行代码来修复错误
let s = String::from("hello, ");
borrow_object(&mut s)
}
fn borrow_object(s: &mut String) {}
```
2022-03-02 08:11:56 -06:00
9. 🌟🌟 Ok: 从可变对象借用不可变
2022-02-25 08:57:13 -06:00
```rust,editable
// 下面的代码没有任何错误
fn main() {
let mut s = String::from("hello, ");
borrow_object(&s);
s.push_str("world");
}
fn borrow_object(s: &String) {}
```
### NLL
2022-03-02 08:11:56 -06:00
10. 🌟🌟
2022-02-25 08:57:13 -06:00
```rust,editable
// 注释掉一行代码让它工作
fn main() {
let mut s = String::from("hello, ");
let r1 = &mut s;
r1.push_str("world");
let r2 = &mut s;
r2.push_str("!");
println!("{}",r1);
}
```
2022-03-02 08:11:56 -06:00
11. 🌟🌟
2022-02-25 08:57:13 -06:00
```rust,editable
fn main() {
let mut s = String::from("hello, ");
let r1 = &mut s;
let r2 = &mut s;
// 在下面增加一行代码人为制造编译错误cannot borrow `s` as mutable more than once at a time
// 你不能同时使用 r1 和 r2
}
2022-03-01 08:06:38 -06:00
```
2022-06-23 00:10:34 -05:00
> 你可以在[这里](https://github.com/sunface/rust-by-practice/blob/master/solutions/ownership/borrowing.md)找到答案(在 solutions 路径下)