小编Zar*_*oix的帖子

为什么在函数调用完成后,可变借用的生命周期不会结束?

我正在为halite.io写一个机器人,并且在理解借用的一些影响方面遇到了问题.这是无法编译的代码:

let scanLoc = hlt::types::Location {
    x: oflow(coord.0 + l.x as i32, game_map.width),
    y: oflow(coord.1 + l.y as i32, game_map.width),
};
let scan = game_map.get_site(scanLoc, types::STILL);
if (&scan.owner != id) | (scan.owner != 0u8) {
    let ang = game_map.get_angle(l, scanLoc);
    debug!("angle b/w: {}", ang);
    return (l, 2);
}
Run Code Online (Sandbox Code Playgroud)

这是编译器错误:

error[E0502]: cannot borrow `*game_map` as immutable because it is also borrowed as mutable
   --> src/MyBot.rs:112:27
      |
  110 |             let scan = game_map.get_site(scanLoc, types::STILL);
      |                        -------- mutable borrow occurs here
  111 | …
Run Code Online (Sandbox Code Playgroud)

rust

3
推荐指数
1
解决办法
926
查看次数

标签 统计

rust ×1