Recently I've been trying to develop a Rust version of the game Super Star Trek. However, I've ran into a slight issue in loading saved games.
To load a game, I'm reading the save file into a string, splitting it into Vec<&str> at (with \0x1e as the seperator), and then using serde_json::from_str on the two resulting elements to get my game data objects (one for the Enterprise, another for the universe).
Unfortunately, when I try to return them I get the following error:
57 | return Some((ent, uni))
| ^^^^^^^^^^^^^^^^ returns a value referencing data owned by the current function
From what I can tell, this is because the data from .split.collect() is something called a "Temporary."
How do I make it possible to return the structs?
EDIT: Here's the github repo: https://github.com/thescribe11/Rust-SST
My code:
use crate::structs::{Enterprise, Universe};
use std::fs::{File};
use std::io::{Read, Write, stdin, stdout};
use serde_json::{to_string, from_str};
<snip>
pub fn get_config_from_file<'a> () -> Option<(Enterprise, Universe<'a>)>{
//! Thaw a game.
//!
//! The .sst file type is as follows:
//! <json data for Enterprise object>\0x1e<json data for Universe object>
let mut save_file: File;
loop {
let temp = File::open(input("Save file: "));
match temp {
Ok(p) => {save_file = p; break;},
Err(e) => {println!("Unable to find save file.\n"); continue;}
};
}
let pass = input("Password: ");
let mut enc_data = String::new();
match save_file.read_to_string(&mut enc_data) {
Ok(_) => {},
Err(_) => {println!("\nERROR: The save file is corrupted."); return None}
}
let mut ent: Enterprise; let mut uni: Universe;
let raw_parts: Vec<&str> = enc_data.split("\0x1e").collect();
ent = match serde_json::from_str(raw_parts[0]) {
Ok(data) => {data},
Err(_) => {println!("\nERROR: The save file is corrupted."); return None}
};
uni = match serde_json::from_str(raw_parts[1]) {
Ok(data) => data,
Err(_) => {println!("\nERROR: The save file is corrupted."); return None}
};
return Some((ent, uni))
}