This is my first year doing Advent of Code and I'm starting to really love working with iterators in Rust. :)
I finished the second part of day 2, but I ran into a minor issue that I really didn't look further into or try to "fix" but it sorta caught my attention. When I chain .map() calls here, it seems to have an issue with the lifetime of the stuff returned from the previous call to .map()...? What is this for<'a> fn(&'a T) -> _ type it's talking about? Is there some way to make this work while still having the chain or is my hacky fix the only way?
fn main() {
let input = inputfile!("2.txt");
// vvv WORKING CODE vvv
let sum = input
.lines()
.map(|line| Game::from_line(line).required_counts().power())
.sum::<u32>();
let sum = input
.lines()
.map(|line| Game::from_line(line).required_counts())
.map(RGB::power)
.sum::<u32>();
// BROKEN ^^^
// Diagnostics:
// 1. type mismatch in function arguments
// expected function signature `fn(RGB) -> _`
// found function signature `for<'a> fn(&'a RGB) -> _` [E0631]
// 2. required by a bound introduced by this call [E0631]
let sum = input
.lines()
.map(Game::from_line)
.map(Game::required_counts)
.map(RGB::power)
.sum::<u32>();
// BROKEN ^^^
// Diagnostics:
// 1. the method `map` exists for struct `Map<Map<Lines<'_>, fn(&str) ->
// Game {Game::from_line}>, fn(&Game) -> RGB {Game::required_counts}>`, but its trait bounds were not satisfied
// the following trait bounds were not satisfied:
// `Map<Map<std::str::Lines<'_>, for<'a> fn(&'a str) -> Game {Game::from_line}>, for<'a> fn(&'a Game) -> RGB {Game::required_counts}>: Iterator`
// which is required by `&mut Map<Map<std::str::Lines<'_>, for<'a> fn(&'a str) ->
// Game {Game::from_line}>, for<'a> fn(&'a Game) -> RGB {Game::required_counts}>: Iterator` [E0599]
println!("{}", sum);
}