mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-23 12:39:42 +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() {}
|
||||
```
|
Reference in New Issue
Block a user