add chapter: fighting with compiler

This commit is contained in:
sunface 2022-03-15 13:32:02 +08:00
parent 136d44149b
commit 346fba581b
5 changed files with 65 additions and 3 deletions

View File

@ -0,0 +1,26 @@
1.
```rust
struct test {
list: Vec<i32>,
a: i32
}
impl test {
pub fn new() -> Self {
test { list:vec![1,2,3,4,5,6,7], a:0 }
}
pub fn run(&mut self) {
for i in 0..self.list.len() {
self.do_something(self.list[i])
}
}
pub fn do_something(&mut self, n: i32) {
self.a = n;
}
}
fn main() {}
```

View File

@ -39,7 +39,7 @@
- [Result and panic](result-panic/intro.md)
- [panic!](result-panic/panic.md)
- [Result and ?](result-panic/result.md)
- [Crate and module](crate-module/intro.md)
- [Crate and Module](crate-module/intro.md)
- [Package and Crate](crate-module/crate.md)
- [Module](crate-module/module.md)
- [Advanced use and pub](crate-module/use-pub.md)
@ -84,4 +84,7 @@
- [Stream](async/stream.md)
- [Standard Library TODO](std/intro.md)
- [String](std/String.md)
- [String](std/String.md)
- [Fighting with Compiler](fight-compiler/intro.md)
- [Borrowing](fight-compiler/borrowing.md)

View File

@ -0,0 +1,29 @@
# Borrowing
1. 🌟🌟
```rust,editable
// FIX the error without removing any code line
struct test {
list: Vec<i32>,
a: i32
}
impl test {
pub fn new() -> Self {
test { list:vec![1,2,3,4,5,6,7], a:0 }
}
pub fn run(&mut self) {
for i in self.list.iter() {
self.do_something(*i)
}
}
pub fn do_something(&mut self, n: i32) {
self.a = n;
}
}
fn main() {}
```

View File

@ -0,0 +1,4 @@
# Fighting with Compiler
Fighting with compiler is very common in our daily coding, especially for those unfamiliar with Rust.
This chapter will provide some exercises to help us avoid such cases to lower the steep learning curve.

View File

@ -1,6 +1,6 @@
# Summary
- [关于 pracitce.rs](about.md)
- [关于 pracitce.rs](why-exercise.md)
- [变量绑定与解构](variables.md)
- [基本类型](basic-types/intro.md)
- [数值类型](basic-types/numbers.md)