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

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!

主站蜘蛛池模板: 塔河县| 毕节市| 邹平县| 霍城县| 会泽县| 邵阳市| 抚顺市| 唐海县| 海南省| 郓城县| 隆子县| 大方县| 天镇县| 荥经县| 科尔| 岱山县| 萨迦县| 大港区| 云安县| 城步| 新化县| 耒阳市| 都安| 新建县| 长葛市| 淄博市| 长寿区| 道真| 南开区| 洛隆县| 称多县| 宁安市| 侯马市| 西藏| 阿拉善盟| 饶河县| 丹寨县| 辽阳县| 洛浦县| 永兴县| 沛县|