官术网_书友最值得收藏!

Saving/loading high scores

To keep things simple, we'll have a very simple file format:

  • On the first line, we store the best scores
  • On the second line, we store the highest number of lines

Let's start by writing the save function:

    fn slice_to_string(slice: &[u32]) -> String {
      slice.iter().map(|highscore| highscore.to_string()).
collect::<Vec<String>>().join(" ") } fn save_highscores_and_lines(highscores: &[u32],
number_of_lines: &[u32]) -> bool { let s_highscores = slice_to_string(highscores); let s_number_of_lines = slice_to_string(number_of_lines); write_into_file(format!("{}\n{}\n", s_highscores,
s_number_of_lines)).is_ok() }

It was a small lie: there are actually two functions. The first one is just here to make the code smaller and easier to read even though we need to explain what it does, because we're about to talk about a big feature from Rust—iterators!

主站蜘蛛池模板: 苍山县| 马关县| 雅安市| 灵寿县| 清水河县| 潜山县| 抚顺市| 固阳县| 昭苏县| 泰顺县| 喜德县| 万山特区| 宁陵县| 商丘市| 买车| 永兴县| 礼泉县| 巫山县| 汪清县| 阿拉善盟| 桃江县| 黎城县| 泰安市| 金阳县| 抚宁县| 河津市| 万年县| 张北县| 镇赉县| 正镶白旗| 聂拉木县| 安平县| 左权县| 雷山县| 新营市| 天祝| 祥云县| 山阴县| 浙江省| 包头市| 铜鼓县|