mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-27 06:29:42 +00:00
update project structure
This commit is contained in:
@ -1,166 +0,0 @@
|
||||
# Reference and Borrowing
|
||||
|
||||
### Reference
|
||||
🌟
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
let x = 5;
|
||||
// fill the blank
|
||||
let p = __;
|
||||
|
||||
println!("the memory address of x is {:p}", p); // output: 0x16fa3ac84
|
||||
}
|
||||
```
|
||||
|
||||
🌟
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
let x = 5;
|
||||
let y = &x;
|
||||
|
||||
// modify this line only
|
||||
assert_eq!(5, y);
|
||||
}
|
||||
```
|
||||
|
||||
🌟
|
||||
```rust,editable
|
||||
|
||||
// fix error
|
||||
fn main() {
|
||||
let mut s = String::from("hello, ");
|
||||
|
||||
borrow_object(s)
|
||||
}
|
||||
|
||||
fn borrow_object(s: &String) {}
|
||||
```
|
||||
|
||||
🌟
|
||||
```rust,editable
|
||||
|
||||
// fix error
|
||||
fn main() {
|
||||
let mut s = String::from("hello, ");
|
||||
|
||||
borrow_object(&s)
|
||||
}
|
||||
|
||||
fn borrow_object(s: &mut String) {}
|
||||
```
|
||||
|
||||
🌟🌟
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
let mut s = String::from("hello, ");
|
||||
|
||||
// fill the blank to make it work
|
||||
let p = __;
|
||||
|
||||
p.push_str("world");
|
||||
}
|
||||
```
|
||||
|
||||
#### ref
|
||||
`ref` can be used to take references to a value, similar to `&`.
|
||||
|
||||
🌟🌟🌟
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
let c = '中';
|
||||
|
||||
let r1 = &c;
|
||||
// fill the blank,dont change other code
|
||||
let __ 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)
|
||||
}
|
||||
```
|
||||
|
||||
### Borrowing rules
|
||||
🌟
|
||||
```rust,editable
|
||||
|
||||
// remove something to make it work
|
||||
// don't remove a whole line !
|
||||
fn main() {
|
||||
let mut s = String::from("hello");
|
||||
|
||||
let r1 = &mut s;
|
||||
let r2 = &mut s;
|
||||
|
||||
println!("{}, {}", r1, r2);
|
||||
}
|
||||
```
|
||||
|
||||
#### Mutablity
|
||||
🌟 Error: Borrow a immutable object as mutable
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
//fix error by modifying this line
|
||||
let s = String::from("hello, ");
|
||||
|
||||
borrow_object(&mut s)
|
||||
}
|
||||
|
||||
fn borrow_object(s: &mut String) {}
|
||||
```
|
||||
|
||||
🌟🌟 Ok: Borrow a mutable object as immutable
|
||||
```rust,editable
|
||||
|
||||
// this code has no errors!
|
||||
fn main() {
|
||||
let mut s = String::from("hello, ");
|
||||
|
||||
borrow_object(&s);
|
||||
|
||||
s.push_str("world");
|
||||
}
|
||||
|
||||
fn borrow_object(s: &String) {}
|
||||
```
|
||||
|
||||
### NLL
|
||||
🌟🌟
|
||||
```rust,editable
|
||||
|
||||
// comment one line to make it work
|
||||
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);
|
||||
}
|
||||
```
|
||||
|
||||
🌟🌟🌟
|
||||
```rust,editable
|
||||
|
||||
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
|
||||
}
|
||||
```
|
@ -1,5 +0,0 @@
|
||||
# Ownership and Borrowing
|
||||
Learning resources:
|
||||
- English: [Rust Book 4.1-4.4](https://doc.rust-lang.org/book/ch04-00-understanding-ownership.html)
|
||||
- 简体中文: [Rust语言圣经 - 所有权与借用(https://course.rs/basic/ownership/index.html)
|
||||
|
@ -1,163 +0,0 @@
|
||||
# Ownership
|
||||
|
||||
🌟🌟
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
// modify this line only! use as many approaches as you can
|
||||
let x = String::from("hello, world");
|
||||
let y = x;
|
||||
println!("{},{}",x,y);
|
||||
}
|
||||
```
|
||||
|
||||
🌟🌟
|
||||
```rust,editable
|
||||
// Don't modify code in main!
|
||||
fn main() {
|
||||
let s1 = String::from("hello, world");
|
||||
let s2 = take_ownership(s1);
|
||||
|
||||
println!("{}", s2);
|
||||
}
|
||||
|
||||
// Only modify the code below!
|
||||
fn take_ownership(s: String) {
|
||||
println!("{}", s);
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
🌟🌟
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
let s = give_ownership();
|
||||
println!("{}", s);
|
||||
}
|
||||
|
||||
// Only modify the code below!
|
||||
fn give_ownership() -> String {
|
||||
let s = String::from("hello, world");
|
||||
// convert String to Vec
|
||||
let _s = s.into_bytes();
|
||||
s
|
||||
}
|
||||
```
|
||||
|
||||
🌟🌟
|
||||
```rust,editable
|
||||
// use clone to fix it
|
||||
fn main() {
|
||||
let s = String::from("hello, world");
|
||||
|
||||
print_str(s);
|
||||
|
||||
println!("{}", s);
|
||||
}
|
||||
|
||||
fn print_str(s: String) {
|
||||
println!("{}",s)
|
||||
}
|
||||
```
|
||||
|
||||
🌟🌟
|
||||
```rust, editable
|
||||
// don't use clone ,use copy instead
|
||||
fn main() {
|
||||
let x = (1, 2, (), "hello");
|
||||
let y = x.clone();
|
||||
println!("{:?}, {:?}", x, y);
|
||||
}
|
||||
```
|
||||
|
||||
#### Mutability
|
||||
Mutability can be changed when ownership is transferred.
|
||||
|
||||
🌟
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
let s = String::from("hello, ");
|
||||
|
||||
// modify this line only !
|
||||
let s1 = s;
|
||||
|
||||
s1.push_str("world")
|
||||
}
|
||||
```
|
||||
|
||||
🌟🌟🌟
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
let x = Box::new(5);
|
||||
|
||||
let ... // implement this line, dont change other lines!
|
||||
|
||||
*y = 4;
|
||||
|
||||
assert_eq!(*x, 5);
|
||||
}
|
||||
```
|
||||
|
||||
### Partial move
|
||||
Within the destructuring of a single variable, both by-move and by-reference pattern bindings can be used at the same time. Doing this will result in a partial move of the variable, which means that parts of the variable will be moved while other parts stay. In such a case, the parent variable cannot be used afterwards as a whole, however the parts that are only referenced (and not moved) can still be used.
|
||||
|
||||
#### Example
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
#[derive(Debug)]
|
||||
struct Person {
|
||||
name: String,
|
||||
age: Box<u8>,
|
||||
}
|
||||
|
||||
let person = Person {
|
||||
name: String::from("Alice"),
|
||||
age: Box::new(20),
|
||||
};
|
||||
|
||||
// `name` is moved out of person, but `age` is referenced
|
||||
let Person { name, ref age } = person;
|
||||
|
||||
println!("The person's age is {}", age);
|
||||
|
||||
println!("The person's name is {}", name);
|
||||
|
||||
// Error! borrow of partially moved value: `person` partial move occurs
|
||||
//println!("The person struct is {:?}", person);
|
||||
|
||||
// `person` cannot be used but `person.age` can be used as it is not moved
|
||||
println!("The person's age from person struct is {}", person.age);
|
||||
}
|
||||
```
|
||||
|
||||
#### Exercises
|
||||
|
||||
🌟
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
let t = (String::from("hello"), String::from("world"));
|
||||
|
||||
let _s = t.0;
|
||||
|
||||
// modify this line only, don't use `_s`
|
||||
println!("{:?}", t);
|
||||
}
|
||||
```
|
||||
|
||||
🌟🌟
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
let t = (String::from("hello"), String::from("world"));
|
||||
|
||||
// fill the blanks
|
||||
let (__, __) = t;
|
||||
|
||||
println!("{:?}, {:?}, {:?}", s1, s2, t);
|
||||
}
|
||||
```
|
Reference in New Issue
Block a user