Rust:如何从文件中获取输入并进行计算?

时间:2019-12-11 05:32:46

标签: rust

我只需要找到一种使用输入进行简单数学运算的方法即可。 advent of code 2019 day 1

我已经尝试了几天
use std::fs::File;
use std::io::{BufRead, BufReader, Error};

fn main() -> Result<(), Error> {
    let path = "input.txt";

    let input = File::open(path)?;
    let buffered = BufReader::new(input);

    for line in buffered.lines() {
        line.parse::<i32>().unwrap();
        line / 2;
        println!("{:?}", line);
    }

    Ok(())
}

1 个答案:

答案 0 :(得分:0)

如果要编译代码。

use std::fs::File;
use std::io::{BufRead, BufReader, Error};

fn main() -> Result<(), Error> {
    let path = "input.txt";

    let input = File::open(path)?;
    let buffered = BufReader::new(input);

    for line in buffered.lines() {
        println!("{:?}", line.unwrap().parse::<i32>().unwrap() / 2);
    }

    Ok(())
}