How can code a good getter and setter mod/lib?


no later than PHP5 my coder generation has to learned that some code are not state of the art. In my prottyping phase of my private project i have a good flashback to the good old days (i mean the 90’s) and have testet some in-main-functions (so i call simple functions in the, now i want to put my quirky code into an better project organisation and my first try is my simple xml replacer function.

I have a simple raw string and in this raw string i want some replacing.

mod XML;
let XMLFile = (r#"Some {Tags] that i want to <h1>to {replace}</h1>"#);
let XMLFile = &XML::replaceTags(XMLFile.to_string(),"Tags".to_string(),"SomeWhat".to_string());
println!("{:?}", XMLFile);

pub fn replaceTags(XMLFile:String,Tag:String,Text:String) -> String
	XMLFile.replace (&Tag,&Text);

Now my Question is. How can is use a oop-getter and oop-setter functionality in mods? Are this possible, why i don’t have a class with class variables and methods. Yes this functioned very well but i want an oop design pattern structure.

I want an XML::set function for setting the XMLFile String, an get function for getting back the modified XMLFile and the replace function for replace in an XMLFile Var that was previously set by that function.

Hmm i remember me that variables only can bet set in functions so my idea nesting an Function-Mod in the XML-Mod and setting an Module-Variable like an Object/Class-Variables cannot be functioned.

How can i simple create something like Classes in other oop-langs? Something is at the moment blocked in me, when i see no ConfFile = new XMFile, CoolFile = new XMLFile. Before i code an quirky framework i ask here:).

My first time i extend my Prototyping (thats functioned very well but is quirky:)):).

Modules are for splitting a project into files (similar to namespaces in PHP), so you can’t have a setter/getter for a module, because there’s no such thing.

You can have getter/setter for a struct, if you add it in impl block. Often it’s best not to add getters and setters unless it’s necessary, because they increase the scope of the borrow.

BTW: I see you’re using String in a parameter, where you need &str. You can use cargo clippy to get suggestions about such things. I also recommend reading more about ownership in Rust, as it’s going to be important.

@kornel Thx for your reply. Yes every day with rust lets comes me closer to the rust-“religion”:). At the moment the last years as an php-developer are forming my thinking. And now i can say “Design Pattern increase the scope of the borrow” :slight_smile:

Ah yeah i use Strings and borrow me the Variables with a .to_string() or other convertig methods. I need pure Strings but i know when i use &str the rust way is a little bit easier. This are my echoes from the good old days:).

The module system gives me an good control and i found it very good. Rust let me no make misstakes anymore also by the simple includes:).

I install me cargo clippy, thx for the tip.

At the moment i let me leading by the compilers warning, when he says a function has snacked the variable i copy the variable, i borrow or i convert to an String :slight_smile: but i know i must go deeper in the ownership system.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.