2022-02-23 00:49:02 -06:00
# Reference and Borrowing
2022-02-25 08:57:13 -06:00
### Reference
2022-03-02 06:57:13 -06:00
1. π
2022-02-25 08:57:13 -06:00
```rust,editable
fn main() {
let x = 5;
2022-04-07 04:52:02 -05:00
// Fill the blank
2022-02-25 08:57:13 -06:00
let p = __ ;
2022-04-07 04:52:02 -05:00
println!("the memory address of x is {:p}", p); // One possible output: 0x16fa3ac84
2022-02-25 08:57:13 -06:00
}
```
2022-03-02 06:57:13 -06:00
2. π
2022-02-25 08:57:13 -06:00
```rust,editable
fn main() {
let x = 5;
let y = &x;
2022-04-07 04:52:02 -05:00
// Modify this line only
2022-02-25 08:57:13 -06:00
assert_eq!(5, y);
2022-03-05 00:48:43 -06:00
2022-04-07 04:52:02 -05:00
println!("Success!");
2022-02-25 08:57:13 -06:00
}
```
2022-03-02 06:57:13 -06:00
3. π
2022-02-25 08:57:13 -06:00
```rust,editable
2022-04-07 04:52:02 -05:00
// Fix error
2022-02-25 08:57:13 -06:00
fn main() {
let mut s = String::from("hello, ");
2022-03-05 00:53:49 -06:00
borrow_object(s);
2022-04-07 04:52:02 -05:00
println!("Success!");
2022-02-25 08:57:13 -06:00
}
fn borrow_object(s: & String) {}
```
2022-03-02 06:57:13 -06:00
4. π
2022-02-25 08:57:13 -06:00
```rust,editable
2022-04-07 04:52:02 -05:00
// Fix error
2022-02-25 08:57:13 -06:00
fn main() {
let mut s = String::from("hello, ");
2022-03-05 00:48:43 -06:00
push_str(s);
2022-04-07 04:52:02 -05:00
println!("Success!");
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 06:57:13 -06:00
5. ππ
2022-02-25 08:57:13 -06:00
```rust,editable
fn main() {
let mut s = String::from("hello, ");
2022-04-07 04:52:02 -05:00
// Fill the blank to make it work
2022-02-25 08:57:13 -06:00
let p = __ ;
p.push_str("world");
2022-03-05 00:48:43 -06:00
2022-04-07 04:52:02 -05:00
println!("Success!");
2022-02-25 08:57:13 -06:00
}
```
2022-04-07 04:52:02 -05:00
#### Ref
2022-02-25 08:57:13 -06:00
`ref` can be used to take references to a value, similar to `&` .
2022-03-02 06:57:13 -06:00
6. πππ
2022-02-25 08:57:13 -06:00
```rust,editable
fn main() {
let c = 'δΈ';
let r1 = &c;
2022-04-07 04:52:02 -05:00
// Fill the blankοΌ dont change other code
2022-02-25 08:57:13 -06:00
let __ r2 = c;
assert_eq!(*r1, *r2);
2022-04-07 04:52:02 -05:00
// Check the equality of the two address strings
2022-02-25 08:57:13 -06:00
assert_eq!(get_addr(r1),get_addr(r2));
2022-03-05 00:48:43 -06:00
2022-04-07 04:52:02 -05:00
println!("Success!");
2022-02-25 08:57:13 -06:00
}
2022-04-07 04:52:02 -05:00
// Get memory address string
2022-02-25 08:57:13 -06:00
fn get_addr(r: & char) -> String {
format!("{:p}", r)
}
```
### Borrowing rules
2022-03-02 06:57:13 -06:00
7. π
2022-02-25 08:57:13 -06:00
```rust,editable
2022-04-07 04:52:02 -05:00
// Remove something to make it work
// Don't remove a whole line !
2022-02-25 08:57:13 -06:00
fn main() {
let mut s = String::from("hello");
let r1 = & mut s;
let r2 = & mut s;
println!("{}, {}", r1, r2);
2022-03-05 00:48:43 -06:00
2022-04-07 04:52:02 -05:00
println!("Success!");
2022-02-25 08:57:13 -06:00
}
```
2022-04-07 04:52:02 -05:00
#### Mutability
8. π Error: Borrow an immutable object as mutable
2022-02-25 08:57:13 -06:00
```rust,editable
fn main() {
2022-04-07 04:52:02 -05:00
// Fix error by modifying this line
2022-02-25 08:57:13 -06:00
let s = String::from("hello, ");
2022-03-05 00:54:28 -06:00
borrow_object(& mut s);
2022-03-05 00:48:43 -06:00
2022-04-07 04:52:02 -05:00
println!("Success!");
2022-02-25 08:57:13 -06:00
}
fn borrow_object(s: & mut String) {}
```
2022-03-02 06:57:13 -06:00
9. ππ Ok: Borrow a mutable object as immutable
2022-02-25 08:57:13 -06:00
```rust,editable
2022-04-07 04:52:02 -05:00
// This code has no errors!
2022-02-25 08:57:13 -06:00
fn main() {
let mut s = String::from("hello, ");
borrow_object(&s);
s.push_str("world");
2022-03-05 00:48:43 -06:00
2022-04-07 04:52:02 -05:00
println!("Success!");
2022-02-25 08:57:13 -06:00
}
fn borrow_object(s: & String) {}
```
### NLL
2022-03-02 06:57:13 -06:00
10. ππ
2022-02-25 08:57:13 -06:00
```rust,editable
2022-04-07 04:52:02 -05:00
// Comment one line to make it work
2022-02-25 08:57:13 -06:00
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 06:57:13 -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;
2022-04-07 04:52:02 -05:00
// Add one line below to make a compiler error: cannot borrow `s` as mutable more than once at a time
// You can't use r1 and r2 at the same time
2022-02-25 08:57:13 -06:00
}
2022-03-01 08:06:38 -06:00
```
2023-01-20 06:01:54 -06:00
> You can find the solutions [here](https://github.com/sunface/rust-by-practice/blob/master/solutions/ownership/borrowing.md)(under the solutions path), but only use it when you need it