From 5155b62b06808b4335e372e51755513efc5ed30a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Autin?= Date: Fri, 10 Mar 2023 10:32:20 +0100 Subject: [PATCH] Added proper error handling to extract_memory_method --- src/simulator/machine.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/simulator/machine.rs b/src/simulator/machine.rs index 55d13ab..48ff028 100644 --- a/src/simulator/machine.rs +++ b/src/simulator/machine.rs @@ -150,8 +150,16 @@ impl Machine { /// /// - **machine** contains the memory pub fn extract_memory(machine: &mut Machine){ - let mut file = File::create("burritos_memory.txt").unwrap(); - file.write(&machine.main_memory); + let file_path = "burritos_memory.txt"; + let write_to_file = |path| -> std::io::Result { + let mut file = File::create(path)?; + file.write_all(&machine.main_memory)?; + Ok(file) + }; + match write_to_file(file_path) { + Err(e) => eprintln!("Failed to write memory to file: {}", e), + Ok(_) => println!("Memory extracted to {}", file_path) + }; } pub fn print_machine_status(machine: &mut Machine) {