New Rustacean here.
I try to learn and grasp Rust idioms by solving Advent of Code puzzle.
WARNING Spoiler: This post contains spoiler for Advent Of Code 2018 - day 01!
Short description part one:
The first part consists in parsing a list of integers (one for each line) and sum them.
example: "1\n2\n3" => 6
Short description part two:
The second part consists in parsing the same list of integers, add them one by one (repeating endlessly if necessary), and find the first number appearing twice.
example: "1\n2\n-2" => 1
Full puzzle description and full code implementation is available at AoC_2018_01
I solved the puzzle and i would like to get a code review of my solution. I am interested in idiomatic Rust.
My solutions:
/// Result type
pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
/// Solution for puzzle part one.
pub fn part_one(input: &str) -> Result<i32> {
let frequency = input
.trim()
.lines()
.filter_map(|x| x.parse::<i32>().ok())
.sum();
Ok(frequency)
}
/// Solution for puzzle part two.
pub fn part_two(input: &str) -> Result<i32> {
let mut iter = input.trim().lines().cycle();
let mut frequency = 0;
let mut set = HashSet::new();
set.insert(frequency);
loop {
let change = iter.next().unwrap().parse::<i32>().unwrap();
frequency += change;
if set.contains(&frequency) {
break Ok(frequency);
} else {
set.insert(frequency);
}
}
}
Thanks for your help.