January 2018
Beginner to intermediate
454 pages
10h 8m
English
As you will certainly have guessed at this point, we'll use iterators once again. This is what the loading function will look like:
fn line_to_slice(line: &str) -> Vec<u32> { line.split(" ").filter_map(|nb| nb.parse::<u32>().ok()).collect() } fn load_highscores_and_lines() -> Option<(Vec<u32>, Vec<u32>)> { if let Ok(content) = read_from_file("scores.txt") { let mut lines = content.splitn(2, "\n").map(|line| line_to_slice(line)).collect::<Vec<_>>(); if lines.len() == 2 { let (number_lines, highscores) = (lines.pop().unwrap(), lines.pop().unwrap()); Some((highscores, number_lines)) } else { None } } else { None }}
Once again, not easy to understand, at first sight. So let's explain all this!
fn line_to_slice(line: ...Read now
Unlock full access