#![allow(dead_code, unused_imports)] use std::fs::File; use std::io::{self, BufRead}; use std::path::Path; fn main() { let mut h = 0; let mut v = 0; let mut aim = 0; if let Ok(lines) = read_lines("directions.txt") { for line in lines { if let Ok(val) = line { println!("{}", val); //shadow assignment let val: Vec<&str> = val.split(' ').collect(); let cmd = val[0]; let n = val[1].parse::().unwrap(); match cmd { "forward" => { h += n; v += n * aim } "down" => aim += n, "up" => aim -= n, _ => println!("not a command"), } } } } println!("h: {}, v: {}, final position: {}", h, v, h * v); } fn read_lines

(filename: P) -> io::Result>> where P: AsRef, { let file = File::open(filename)?; Ok(io::BufReader::new(file).lines()) }