首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HashMap Entry应用编程接口的生存期/借用问题

HashMap Entry应用编程接口的生存期/借用问题
EN

Stack Overflow用户
提问于 2020-05-06 05:36:51
回答 1查看 75关注 0票数 0

我想不出如何让下面的代码成功编译。第一步是艰难地通过生命周期注解,然而,我觉得它终于成功了。

下一步是围绕HashMap条目的借用和生存期。我觉得我已经在兔子洞里太深了,需要一些指导才能出来。

代码语言:javascript
复制
use std::collections::HashMap;

struct Player {
    id: u32,
}

struct Game<'a> {
    black: &'a Player,
    white: &'a Player,
    win: bool,
    timestamp: u32,
}

struct Base<'b> {
    games: &'b mut Vec<Game<'b>>,
    players: &'b mut HashMap<u32, Player>,
}

impl<'c> Base<'c> {
    fn create_game(self, black_id: u32, white_id: u32, win: bool, timestamp: u32) -> &'c Game<'c> {
        let black_player = self
            .players
            .entry(black_id)
            .or_insert(Player { id: black_id });
        let white_player = self
            .players
            .entry(white_id)
            .or_insert(Player { id: white_id });

        let game = Game {
            black: &black_player,
            white: &white_player,
            win: win,
            timestamp: timestamp,
        };

        self.games.push(game);
        &self.games[0]
    }
}
代码语言:javascript
复制
error[E0499]: cannot borrow `*self.players` as mutable more than once at a time
  --> src/lib.rs:25:28
   |
19 |    impl<'c> Base<'c> {
   |         -- lifetime `'c` defined here
20 |        fn create_game(self, black_id: u32, white_id: u32, win: bool, timestamp: u32) -> &'c Game<'c> {
21 |            let black_player = self
   |   ____________________________-
   |  |____________________________|
   | ||
22 | ||             .players
   | ||____________________- first mutable borrow occurs here
23 | |              .entry(black_id)
   | |_____________________________- argument requires that `*self.players` is borrowed for `'c`
24 |                .or_insert(Player { id: black_id });
25 |            let white_player = self
   |  _____________________________^
26 | |              .players
   | |_____________________^ second mutable borrow occurs here

error[E0597]: `black_player` does not live long enough
  --> src/lib.rs:31:20
   |
19 | impl<'c> Base<'c> {
   |      -- lifetime `'c` defined here
...
31 |             black: &black_player,
   |                    ^^^^^^^^^^^^^ borrowed value does not live long enough
...
37 |         self.games.push(game);
   |         --------------------- argument requires that `black_player` is borrowed for `'c`
38 |         &self.games[0]
39 |     }
   |     - `black_player` dropped here while still borrowed

error[E0597]: `white_player` does not live long enough
  --> src/lib.rs:32:20
   |
19 | impl<'c> Base<'c> {
   |      -- lifetime `'c` defined here
...
32 |             white: &white_player,
   |                    ^^^^^^^^^^^^^ borrowed value does not live long enough
...
37 |         self.games.push(game);
   |         --------------------- argument requires that `white_player` is borrowed for `'c`
38 |         &self.games[0]
39 |     }
   |     - `white_player` dropped here while still borrowed
EN

回答 1

Stack Overflow用户

发布于 2020-05-06 06:14:22

如果您只想让代码“编译”(在某种意义上“可用于进一步的开发”),而不是试图“学习如何在Rust中执行复杂的生命周期”,那么我建议您使用下面的代码的简单版本。

代码语言:javascript
复制
#[derive(Copy, Clone)]
struct Player {
    id: u32,
}

#[derive(Copy, Clone)]
struct Game {
    black: Player,
    white: Player,
    win: bool,
    timestamp: u32
}

struct Base {
    games: Vec<Game>,
    players: HashMap<u32, Player>
}

impl Base {
    fn create_game(&mut self, black_id: u32, white_id: u32, win: bool, timestamp: u32) -> Game {
        self.players.entry(black_id).or_insert(Player {id: black_id });
        self.players.entry(white_id).or_insert(Player {id: white_id });
        let black_player: &Player = self.players.get(&black_id).unwrap(); // Should be safe to unwrap here
        let white_player: &Player = self.players.get(&white_id).unwrap(); // Should be safe to unwrap here

        let game = Game {
            black: *black_player,
            white: *white_player,
            win: win,
            timestamp: timestamp
        };

        self.games.push(game);
        self.games[0]
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61623518

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档