From 8b1158a520d91765fa5a2bc832d13b5263da88cd Mon Sep 17 00:00:00 2001 From: Yohan Boujon Date: Tue, 10 Oct 2023 16:05:32 +0200 Subject: [PATCH] Reading the file --- day1/elves | 14 ++++++++++++++ day1/src/elves.rs | 18 ++++++++++++++++++ day1/src/main.rs | 40 +++++++++++++++++++++------------------- 3 files changed, 53 insertions(+), 19 deletions(-) create mode 100644 day1/elves create mode 100644 day1/src/elves.rs diff --git a/day1/elves b/day1/elves new file mode 100644 index 0000000..444e241 --- /dev/null +++ b/day1/elves @@ -0,0 +1,14 @@ +1000 +2000 +3000 + +4000 + +5000 +6000 + +7000 +8000 +9000 + +10000 \ No newline at end of file diff --git a/day1/src/elves.rs b/day1/src/elves.rs new file mode 100644 index 0000000..46b28e9 --- /dev/null +++ b/day1/src/elves.rs @@ -0,0 +1,18 @@ +pub struct Elf { + inventory : Vec, +} + +impl Elf { + pub fn new(vec : Vec) -> Elf { + Elf { + inventory: vec, + } + } + + pub fn print_inventory(& self) + { + for inv in &self.inventory { + println!("{}",inv) + } + } +} \ No newline at end of file diff --git a/day1/src/main.rs b/day1/src/main.rs index 9a06774..111ac74 100644 --- a/day1/src/main.rs +++ b/day1/src/main.rs @@ -1,28 +1,30 @@ -pub struct Elf { - inventory : Vec, -} +use std::fs::File; +use std::io::Read; +mod elves; -impl Elf { - pub fn new(vec : Vec) -> Elf { - Elf { - inventory: vec, - } - } - - pub fn print_inventory(& self) - { - for inv in &self.inventory { - println!("{}",inv) - } - } -} fn main() { - let elves = vec![Elf::new(vec![1000,2000,3000]),Elf::new(vec![4000]), Elf::new(vec![5000,6000]), Elf::new(vec![7000,8000,9000]), Elf::new(vec![10000])]; - for elf in &elves + let elf_vec = vec![elves::Elf::new(vec![1000,2000,3000]),elves::Elf::new(vec![4000]), elves::Elf::new(vec![5000,6000]), elves::Elf::new(vec![7000,8000,9000]), elves::Elf::new(vec![10000])]; + for elf in &elf_vec { println!("Elf :"); elf.print_inventory() } + + let mut file; + // Checking if the file is found. + match File::open("elves") { + Ok(actualfile) => { + file = actualfile; + } + Err(fileerr) => { + panic!("Could not open file 'elves' : {}", fileerr) + } + }; + + // There will be content if the file is read so we unwrap. + let mut file_content = String::new(); + file.read_to_string(&mut file_content).unwrap(); + println!("{}", file_content); }