- Rust Programming By Example
- Guillaume Gomez Antoni Boucher
- 119字
- 2021-07-02 19:13:07
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!
推薦閱讀
- 情報(bào)學(xué)與信息管理的主戰(zhàn)場(chǎng):北京大學(xué)情報(bào)學(xué)與信息管理論壇文集
- 地方檔案與文獻(xiàn)研究(第2輯)
- 單讀. 十周年特輯(時(shí)間的移民+在世界的門外)共2冊(cè)
- 國外圖書館動(dòng)漫資源建設(shè)與服務(wù)
- 中國古代圖書館學(xué)研究
- 網(wǎng)絡(luò)學(xué)術(shù)信息老化及應(yīng)對(duì)管理
- 醫(yī)學(xué)文獻(xiàn)管理
- Practical Industrial Internet of Things Security
- 構(gòu)筑閱讀天堂:圖書館服務(wù)設(shè)計(jì)探索
- 邂逅法學(xué)圖書館:浙江大學(xué)光華法學(xué)院師生原創(chuàng)文集
- 基于價(jià)值全面實(shí)現(xiàn)的檔案信息資源配置
- 圖書情報(bào)與圖書館服務(wù)探究
- 魯迅輯校古籍考
- 李一氓文存(第一卷):存在集·存在集續(xù)編
- 武大老照片