Cod sursa(job #2775816)

Utilizator george.tutuianuTutuianu George-Alexandru george.tutuianu Data 17 septembrie 2021 13:03:50
Problema A+B Scor 100
Compilator rs Status done
Runda Arhiva de probleme Marime 0.86 kb
use std::fs;
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
use std::io::Error;
use std::path::Path;

fn main() {
    let path = Path::new("adunare.in");
    let a = get_line_at(&path, 0).unwrap();
    let b = get_line_at(&path, 1).unwrap();

    let int_a = a.parse::<i32>().unwrap();
    let int_b = b.parse::<i32>().unwrap();

    let sum = sum(int_a, int_b);

    fs::write("adunare.out", sum.to_string()).expect("Unable to write file");
}
 
fn get_line_at(path: &Path, line_num: usize) -> Result<String, Error> {
    // Read that exact line from the file
    let file = File::open(path).expect("File not found or cannot be opened");
    let content = BufReader::new(&file);
    let mut lines = content.lines();
    lines.nth(line_num).expect("No line found at that position")
}

fn sum(a: i32, b: i32) -> i32 {
  return a + b
}