小编lok*_*dev的帖子

沙沙声课程的正确答案,但不满意

这是对最高的尼维奥斯的抱怨,但我解决了沙沙声课程中的一项任务,我相信这不是最佳解决方案 - 甚至不是一个好的解决方案。

任务:https://github.com/rust-lang/rustlings/blob/main/exercises/hashmaps/hashmaps3.rs

我的解决方案(仅相关位):

fn build_scores_table(results: String) -> HashMap<String, Team> {
    // The name of the team is the key and its associated struct is the value.
    let mut scores: HashMap<String, Team> = HashMap::new();

    for r in results.lines() {
        let v: Vec<&str> = r.split(',').collect();
        let team_1_name = v[0].to_string();
        let team_1_score: u8 = v[2].parse().unwrap();
        let team_2_name = v[1].to_string();
        let team_2_score: u8 = v[3].parse().unwrap();

        let team_1 = scores.entry(team_1_name.clone()).or_insert(Team {
            name: team_1_name.clone(),
            goals_scored: 0,
            goals_conceded: 0,
        });
        team_1.goals_scored += team_1_score;
        team_1.goals_conceded …
Run Code Online (Sandbox Code Playgroud)

string hashmap rust

7
推荐指数
1
解决办法
2445
查看次数

标签 统计

hashmap ×1

rust ×1

string ×1