From 346fba581b3cab4eb0be0888d431b1a035e62307 Mon Sep 17 00:00:00 2001 From: sunface Date: Tue, 15 Mar 2022 13:32:02 +0800 Subject: [PATCH] add chapter: fighting with compiler --- solutions/fight-compiler/borrowing.md | 26 ++++++++++++++++++++++++ src/SUMMARY.md | 7 +++++-- src/fight-compiler/borrowing.md | 29 +++++++++++++++++++++++++++ src/fight-compiler/intro.md | 4 ++++ zh-CN/src/SUMMARY.md | 2 +- 5 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 solutions/fight-compiler/borrowing.md create mode 100644 src/fight-compiler/borrowing.md create mode 100644 src/fight-compiler/intro.md diff --git a/solutions/fight-compiler/borrowing.md b/solutions/fight-compiler/borrowing.md new file mode 100644 index 0000000..801c8d6 --- /dev/null +++ b/solutions/fight-compiler/borrowing.md @@ -0,0 +1,26 @@ +1. +```rust +struct test { + list: Vec, + 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() {} +``` \ No newline at end of file diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 11ae510..0133fa2 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -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) \ No newline at end of file + - [String](std/String.md) + +- [Fighting with Compiler](fight-compiler/intro.md) + - [Borrowing](fight-compiler/borrowing.md) \ No newline at end of file diff --git a/src/fight-compiler/borrowing.md b/src/fight-compiler/borrowing.md new file mode 100644 index 0000000..3c798d2 --- /dev/null +++ b/src/fight-compiler/borrowing.md @@ -0,0 +1,29 @@ +# Borrowing + +1. 🌟🌟 +```rust,editable +// FIX the error without removing any code line +struct test { + list: Vec, + 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() {} +``` \ No newline at end of file diff --git a/src/fight-compiler/intro.md b/src/fight-compiler/intro.md new file mode 100644 index 0000000..24fffc1 --- /dev/null +++ b/src/fight-compiler/intro.md @@ -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. \ No newline at end of file diff --git a/zh-CN/src/SUMMARY.md b/zh-CN/src/SUMMARY.md index dbff8e8..74dd804 100644 --- a/zh-CN/src/SUMMARY.md +++ b/zh-CN/src/SUMMARY.md @@ -1,6 +1,6 @@ # Summary -- [关于 pracitce.rs](about.md) +- [关于 pracitce.rs](why-exercise.md) - [变量绑定与解构](variables.md) - [基本类型](basic-types/intro.md) - [数值类型](basic-types/numbers.md)