mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-23 04:29:41 +00:00
add chapter: fighting with compiler
This commit is contained in:
26
solutions/fight-compiler/borrowing.md
Normal file
26
solutions/fight-compiler/borrowing.md
Normal 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() {}
|
||||||
|
```
|
@ -39,7 +39,7 @@
|
|||||||
- [Result and panic](result-panic/intro.md)
|
- [Result and panic](result-panic/intro.md)
|
||||||
- [panic!](result-panic/panic.md)
|
- [panic!](result-panic/panic.md)
|
||||||
- [Result and ?](result-panic/result.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)
|
- [Package and Crate](crate-module/crate.md)
|
||||||
- [Module](crate-module/module.md)
|
- [Module](crate-module/module.md)
|
||||||
- [Advanced use and pub](crate-module/use-pub.md)
|
- [Advanced use and pub](crate-module/use-pub.md)
|
||||||
@ -85,3 +85,6 @@
|
|||||||
|
|
||||||
- [Standard Library TODO](std/intro.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)
|
29
src/fight-compiler/borrowing.md
Normal file
29
src/fight-compiler/borrowing.md
Normal 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() {}
|
||||||
|
```
|
4
src/fight-compiler/intro.md
Normal file
4
src/fight-compiler/intro.md
Normal 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.
|
@ -1,6 +1,6 @@
|
|||||||
# Summary
|
# Summary
|
||||||
|
|
||||||
- [关于 pracitce.rs](about.md)
|
- [关于 pracitce.rs](why-exercise.md)
|
||||||
- [变量绑定与解构](variables.md)
|
- [变量绑定与解构](variables.md)
|
||||||
- [基本类型](basic-types/intro.md)
|
- [基本类型](basic-types/intro.md)
|
||||||
- [数值类型](basic-types/numbers.md)
|
- [数值类型](basic-types/numbers.md)
|
||||||
|
Reference in New Issue
Block a user