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

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!

主站蜘蛛池模板: 花莲市| 石景山区| 威信县| 恭城| 枝江市| 苗栗市| 汝阳县| 大姚县| 茶陵县| 尉犁县| 馆陶县| 辽阳县| 勃利县| 永嘉县| 青浦区| 梅州市| 濮阳县| 巨野县| 浙江省| 仪征市| 霍林郭勒市| 靖江市| 积石山| 田阳县| 阳春市| 抚顺市| 定日县| 资源县| 安仁县| 浦城县| 翼城县| 石棉县| 子洲县| 全椒县| 嘉义县| 临泉县| 夹江县| 安泽县| 广平县| 龙海市| 年辖:市辖区|