use std::fs::File;
use std::io::prelude::*;
use std::io::BufReader;
fn input_lines() -> Result<Vec, std::io::Error> {
let f = File::open("src/input.txt")?;
let reader = BufReader::new(f);
let mut result: Vec = Vec::new();
for line in reader.lines() {
result.push(line?)
}
fn get_number_form_str(line: &str) -> i32 {
let first = match get_first(line) {
Some(val) => val,
None => -1,
};
let last = match get_last(line) {
Some(val) => val,
None => -1,
};
//println!("last: {}, fist: {}", last, first);
return (first.to_string() + &last.to_string())
.parse::()
.unwrap();
}
fn main() {
let mut decoded: Vec = Vec::new();
for line in input_lines().unwrap() {
decoded.push(get_number_form_str(&line))
}
println!("{}", decoded.iter().sum::())
}
Assistant
It looks like you have implemented a solution to the problem using Rust. Your code reads the input file, gets the first and last numerical strings from each line, and then converts them into a single number. Finally, it calculates the sum of these numbers. Here's your code with some improvements and comments:
rust
use std::fs::File;
use std::io::prelude::*;
use std::io::BufReader;
fn input_lines() -> Result<Vec<String>, std::io::Error> {
let f = File::open("src/input.txt")?;
let reader = BufReader::new(f);
let mut result: Vec<String> = Vec::new();
for line in reader.lines() {
result.push(line?)
}
return Result::Ok(result);
}
fn get_first(line: &str) -> Option<i32> {
let mut line_copy = line.to_owned().chars().collect::<Vec<char>>();
while !line_copy.is_empty() {