Understanding save informations while rust executable is running

Hello, all functioned very well in my “Give Powershell-Scripts an new home in an rust executable :slight_smile:” Project but with only simple mods and simple functions in that includes. Now i have problem with reusing an Variable while rust is running and this is the time for an fresh starts into the structs/impl mechanism in rust.

pub struct PSScripts
{
	pub data: String,
}
impl PSScripts
{
	pub fn new(ScriptName: &str) -> PSScripts
	{
		let mut scriptdata: String = "fooBar".to_string();
		if (ScriptName == "MakeLnk")
		{
			println!("{:?}", "foo");
			scriptdata = include_str!(r"..\..\MakeLnk.ps1").to_string();
		}
		createTmpScriptFile("MakeLnk.ps1",&scriptdata);
		PSScripts
		{
			data: scriptdata.to_string(),
		}
	}
}

pub fn writeLnk(lnkPath: &str, lnkTarget: &str, lnkArgs: &str, lnkIcon: &str)
{
	use std::process::{Command, Stdio};
	let lnk_script = PSScripts::new ("MakeLnk");

So yes the problem that include! macros only accepts literals i have solved with an classical if statement :slight_smile: and furthermore i will remove the strcut data (is useless) and only put an scriptname struct variable in this. The code above is only for showing my understandings and codings before i come here and quest a question:).

So my problem: I want an in other Languages so called object that i can reuse, when i call the function writeLnk rust don’t reuse the lnk_script variable and include to scriptdata twice and create the scriptfile by every calling the function writeLnk.

So the creation of the scriptfile can i pass by with an file_exist, cool but his passby will be unneccessary when i can rust say that he don’t must recreate the lnk_script variable and reuse this variable every time when i call write_lnk again.

thx

Don’t have rust such as an Singleton Pattern? Or so?

What do you even mean? A pattern is something one could use to achieve certain things, it’s not something that a language has.

1 Like

@hellow Thx for the link, yes thats the excact question. In german i have allways says “Design Pattern are only blue prints for problem and design questions.”. In english i must use sometimes a simple question. But the link answer my questions completly.

I have tested my program if i can save memory when i reuse the variable in any tested ways and the answer is no. The only performance gain i can have is when i check if the file was created and do not at creation every time i call the function.

At the moment i code very oldscool in rust. I have functions and some functions with return variables:) and i know thats not very compatible to publish the code for others. For now i can read rust-docs better and better and i love it when i have clear structures that i can read and use :slight_smile:.

At the moment i should let go my oldscool programmed way and look at the error handling system from rust.