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

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!

主站蜘蛛池模板: 涞源县| 吴堡县| 垫江县| 正宁县| 博罗县| 永兴县| 宁城县| 万宁市| 耒阳市| 闽清县| 北票市| 辉南县| 固始县| 砀山县| 泗洪县| 绍兴县| 巴彦县| 漳平市| 堆龙德庆县| 巴彦淖尔市| 沈阳市| 玛多县| 循化| 和政县| 牙克石市| 平度市| 磐安县| 禄丰县| 全南县| 清镇市| 鹤庆县| 广德县| 连平县| 肇州县| 霸州市| 威海市| 赞皇县| 天峻县| 萍乡市| 赤水市| 卢氏县|