Kind of a continuation of this: Is it ok to use clone when creating a struct from another one? - #8
I have a struct that I am using in a program I am working on, and one thing I am running into quite frequently is having to use clone()
on my struct on each line. Let me show an example:
pub fn run(self) {
let mut data = String::new();
let response = self.clone().validate();
let request = self.clone().generate(response.clone(), &mut data);
self.connect(response.clone(), request);
}
I am still in the process of building this and will be incorporating having to go through a few more for
loops of iterated data. So I assume I will end up having to use clone on self
a few more times.
Here is the actual struct that I essentially want to live the whole program, and be referenced in different places.
pub struct Properties {
pub sub_command: String,
pub word_list: Vec<String>,
pub http_method: String,
pub post_data: String,
pub headers: Vec<String>,
pub exclude_status: Vec<String>,
pub exclude_word_count: Vec<String>,
pub exclude_content_length: Vec<String>,
pub url: String,
}
Am I using clone()
, causing a lot of performance degradation? Would I be better off, using clone() in places where I reference the struct only to get snippets of its data?
// Get hostname from URL string. Validate if port is connected to the end ":443"
let protocol_appended = Regex::new(r"(:\d{2,5})").unwrap();
let mut hostname = String::new();
if protocol_appended.find(&self.properties.url).is_some() {
if self.properties.url.find("://").is_some() {
hostname = self.properties.url.split("://").collect::<Vec<&str>>()[1].split(":").collect::<Vec<&str>>()[0].to_string();
} else {
hostname = self.properties.url.split("/").collect::<Vec<&str>>()[0].split(":").collect::<Vec<&str>>()[0].to_string();
}
} else {
if self.properties.url.find("://").is_some() {
hostname = self.properties.url.split("://").collect::<Vec<&str>>()[1].split("/").collect::<Vec<&str>>()[0].to_string();
} else {
hostname = self.properties.url.split("/").collect::<Vec<&str>>()[0].to_string();
}
}
Any thoughts greatly appreciated!