01-借用

借用 — Rust By Practice

内容来源 · Rust By Practice / Rust 语言实战

原文链接: https://practice-rust-zh.beatai.org/fight-compiler/borrowing.html

借用

  1. 🌟🌟
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// 不删除任何代码,修复错误
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() {}

参考答案:https://github.com/sunface/rust-by-practice/blob/master/solutions/fight-compiler/borrowing.md(solutions 路径),仅在需要时查看。

最后修改 August 21, 2026: 更新 (76fc81a2e)