I had this code that needs to clone a String value to add it to a vector that is then returned at the end of the function:
let file = File::open(entry.path())?;
let mut reader = BufReader::new(file);
let line_buf = RefCell::new(String::new());
while reader.read_line(&mut line_buf)? > 0 {
let line_buf_trim = line_buf.trim();
if ["- [ ]", "- [x]"]
.iter()
.any(|s| line_buf.trim().to_lowercase().starts_with(*s))
|| org_regex.0.is_match(line_buf_trim)
|| org_regex.1.is_match(line_buf_trim)
{
if !file_tasks.contains_key(entry.path()) {
file_tasks.insert(entry.path().to_path_buf(), Vec::new());
}
let Some(tasks) = file_tasks.get_mut(entry.path()) else {
warn!(
"Unable to add task to the list associated with {}",
entry.path().display()
);
continue;
};
tasks.push(line_buf.clone());
}
line_buf.clear();
}
I wanted to try and avoid the potentially expensive String clone so I decided to try and refactor it using RefCell (and I did make an implementation of this) but would it even be worth it? Is there a big difference between a RefCell clone and a regular clone?