rust-by-practice/solutions/ownership/borrowing.md

154 lines
2.0 KiB
Markdown
Raw Normal View History

2022-05-01 00:08:50 -05:00
1.
2022-03-02 06:57:13 -06:00
```rust
fn main() {
let x = 5;
// fill the blank
let p = &x;
println!("the memory address of x is {:p}", p); // one possible output: 0x16fa3ac84
}
```
2022-05-01 00:08:50 -05:00
2.
2022-03-02 06:57:13 -06:00
```rust
fn main() {
let x = 5;
let y = &x;
// modify this line only
assert_eq!(5, *y);
}
```
2022-05-01 00:08:50 -05:00
3.
2022-03-02 06:57:13 -06:00
```rust
fn main() {
let mut s = String::from("hello, ");
borrow_object(&s)
}
fn borrow_object(s: &String) {}
```
2022-05-01 00:08:50 -05:00
4.
2022-03-02 06:57:13 -06:00
```rust
fn main() {
let mut s = String::from("hello, ");
push_str(&mut s)
}
fn push_str(s: &mut String) {
s.push_str("world")
}
```
2022-05-01 00:08:50 -05:00
5.
2022-03-02 06:57:13 -06:00
```rust
fn main() {
let mut s = String::from("hello, ");
// fill the blank to make it work
let p = &mut s;
p.push_str("world");
}
```
2022-05-01 00:08:50 -05:00
6.
2022-03-02 06:57:13 -06:00
```rust
fn main() {
let c = 'äø­';
let r1 = &c;
// fill the blankļ¼Œdont change other code
let ref r2 = c;
assert_eq!(*r1, *r2);
// check the equality of the two address strings
assert_eq!(get_addr(r1),get_addr(r2));
}
// get memory address string
fn get_addr(r: &char) -> String {
format!("{:p}", r)
}
```
2022-05-01 00:08:50 -05:00
7.
2022-03-02 06:57:13 -06:00
```rust
fn main() {
let s = String::from("hello");
let r1 = &s;
let r2 = &s;
println!("{}, {}", r1, r2);
}
```
2022-05-01 00:08:50 -05:00
8.
2022-03-02 06:57:13 -06:00
```rust
fn main() {
//fix error by modifying this line
let mut s = String::from("hello, ");
borrow_object(&mut s)
}
fn borrow_object(s: &mut String) {}
```
2022-05-01 00:08:50 -05:00
9.
2022-03-02 06:57:13 -06:00
```rust
fn main() {
let mut s = String::from("hello, ");
borrow_object(&s);
s.push_str("world");
}
fn borrow_object(s: &String) {}
```
2022-05-01 00:08:50 -05:00
10.
2022-03-02 06:57:13 -06:00
```rust
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-05-01 00:08:50 -05:00
11.
2022-03-02 06:57:13 -06:00
```rust
fn main() {
let mut s = String::from("hello, ");
let r1 = &mut s;
let r2 = &mut s;
// 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
r1.push_str("world");
}
```